好的,这就是我的问题。
我有“page1.php”,包含以下代码:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<textarea id="note-textarea"></textarea>
<script>
$( "#note-textarea" ).keyup( function() {
$( "#output_div" ).html( $( this ).val() );
setTimeout(function () {
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("XMLHTTP");
}
xmlhttp.open("GET", "/upload-note?note="+$('#note-textarea').val(), true);
xmlhttp.send(null);
}, 1000);
});
</script>
</body>
</html>
还有“upload-note.php”,它应该将“page1.php”中文本区域的内容上传到 MySQL 数据库。出于演示目的,我们只是说它将回显文本字段的内容。
<?php
echo($_GET['note']);
?>
现在这个设置实际上工作得很好,但它忽略了换行符。关于如何处理这些问题有什么建议吗?
最佳答案
浏览器会忽略 HTML 文档中的换行符 ( \n
)。您必须使用 <br>
更改它们像这样的标签..
echo nl2br($_GET['note']);
<小时/>
更改为 POST 而不是 GET
xmlhttp.open("POST", "/upload-note", true);
xmlhttp.send("note="+$('#note-textarea').val());
...
echo nl2br($_POST['note']);
关于javascript - PHP - GET url 忽略换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36549377/