使用nl2br用换行符显示数据库中的数据

Displaying data from database with newlines using nl2br

本文关键字:数据 数据库 显示 nl2br 换行符 使用      更新时间:2024-04-07

我不能从前面的问题中得到正确的答案,所以我在这里进一步解释
这是我的index.php,这是我用来将其保存到数据库的方法

<html>
<head>
<script>
function updategroup()
{
var update_con=document.getElementsByName("update_con")[0].value.replace(/''r''n/g, "<br />");
var xmlhttp;
if(update_con == "" || update_con == " ")
{
    alert("Box is empty");
    return;
}
else{
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("groups").innerHTML=xmlhttp.responseText;
        }
    }
xmlhttp.open("GET","fors.php?update_con="+update_con,true);
xmlhttp.send();}
}
</script>
</head>

<body>
<textarea name="update_con" rows="5" cols="50" >
</textarea>
<button onclick="updategroup()" >click</button>
<div id="groups" ><?php include("updates.php"); ?></div>
</body>
</html>

线路

var update_con=document.getElementsByName("update_con")[0].value.replace(/''r''n/g,"
")

我曾经把nl替换成br。我做对了吗?我想不会。

这是fors.php

<?php
include("conn2.php");
$update_con = $_GET["update_con"];
$query_fors = "INSERT INTO updates(update_con) VALUES('$update_con')";
mysql_query($query_fors);
include("updates.php");
?>

最后,我的更新.php

<?php
include("conn2.php");
$query_sel = mysql_query("SELECT * FROM updates");
while($rows_sel = mysql_fetch_assoc($query_sel)){
$update_con = $rows_sel['update_con'];
//echo "<div style='border: 1px solid #000;' >$update_con</div><br />";
echo nl2br(htmlspecialchars($update_con))."<br />";
}
?>

我使用了echo nl2br(htmlspecialchars($update_con))行来显示它。当我在文本区域内输入带有换行符的文本时,我只得到一条直文本,没有换行符。。。。当我打开"fors"数据库中的"updates"表时。我无法像进入文本区那样看到带有换行符的文本。提前感谢:)

我不希望我的数据库包含任何html标签:)

为什么这样:.replace(/''r''n/g,");-这就是消磨你休息时间的原因。只需发送encodeURIComponent(update_con)-Kai Qing