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

时间:2013-12-07 01:47:47

标签: javascript php

我无法从上一个问题中解决问题,所以我在这里进一步解释。
这是我的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))来显示它。当我使用换行符在textarea中输入文本时,我只会得到一个直接的文本,没有中断,没有换行符....当我在'fors'数据库中打开'updates'表时。我无法看到像我进入textarea的新行。提前谢谢:)

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

1 个答案:

答案 0 :(得分:1)

为什么:.replace(/ \ r \ n / g,“”); - 这就是你的休息时间。只需发送encodeURIComponent(update_con) - Kai Qing