![](/img/trans.png)
[英]Get textarea value having linebreaks in Javascript variable using PHP
[英]PHP - GET url ignoring linebreaks
好的,这是我的问题。
我有“ 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”中textarea的内容上载到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']);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.