好吧,这是我的问题。
我有"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>
以及"uploadnote.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']);