duankuixi1930 2013-12-07 01:47
浏览 81
已采纳

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

I can't get it right from my previous question so I explain it here more further.
This is my index.php and that's the method I used to save it to database

<html>

<head>

<script>

function updategroup()
{
var update_con=document.getElementsByName("update_con")[0].value.replace(/\\
/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>

The line

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

I used to replace nl to br. Am I get it right? I guess not.

this is the 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");

?>

And finally, my updates.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 />";
}

?>

I used the line echo nl2br(htmlspecialchars($update_con)) for displaying it. And when I enter texts inside the textarea with newlines, I only get a straight texts,without breaks,without newlines.... And when I open 'updates' table inside 'fors' database. I couldn't see texts with newlines like I enter into textarea. Thanks in advance :)

I don't want my database to hold any html tags :)

  • 写回答

1条回答 默认 最新

  • duanmiaosi0150 2013-12-07 03:13
    关注

    why this: .replace(/ /g, " "); - that's what's killing your breaks. just send encodeURIComponent(update_con) - Kai Qing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP