dongxi1680
2016-04-11 12:58
浏览 133
已采纳

PHP - GET url忽略换行符

Okay, so here's my problem.

I have "page1.php", with following code:

<!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>

And "upload-note.php", which should upload the content of the textarea from "page1.php", to a MySQL database. For demonstration-purpose, let's just say, that it's going to echo the content of the textfield instead.

<?php
    echo($_GET['note']);
?>

Now this setup works actually just fine, BUT it's ignoring linebreaks. Any suggestions on how to handle these?

图片转代码服务由CSDN问答提供 功能建议

好的,所以这是我的问题。

我有“page1.php”,代码如下:

 &lt;!DOCTYPE html&gt; 
&lt; html&gt; 
  &lt; head&gt; 
&lt; script src =“https://code.jquery.com/jquery-1.10.2.js”&gt;&lt; / script&gt; 
&lt; / head&gt; 
&lt; body&gt; \  n 
&lt; textarea id =“note-textarea”&gt;&lt; / textarea&gt; 
 
&lt; script&gt; 
 $(“#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); 
 
}); 
&lt; / script&gt; 
 
&lt; / body&gt; \  n&lt; / html&gt; 
   
 
 

和“upload-note.php”,它应该将textarea的内容从“page1.php”上传到MySQL数据库 。 为了演示目的,我们只是说,它将回应文本字段的内容。

 &lt;?php 
 echo($ _ GET ['note']  ); 
?&gt; 
   
 
 

现在这个设置实际上运行得很好,但它忽略了换行符。 关于如何处理这些的任何建议?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyan2445 2016-04-11 13:00
    已采纳

    The browser ignores linebreaks ( ) in HTML documents. You have to change them with <br> tags like so..

    echo nl2br($_GET['note']);
    

    Change to POST instead of GET

    xmlhttp.open("POST", "/upload-note", true);
    xmlhttp.send("note="+$('#note-textarea').val());
    

    ...

    echo nl2br($_POST['note']);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题