Textarea - 缺少新线

时间:2014-03-02 22:30:35

标签: javascript php html textarea newline

我有以下代码:

<form action="../p/padd.php" method="POST"> 
<input type="button" value="Náhľad"  OnClick="javascript:nahlad()" /> 
<textarea tabindex="4" id="textra" name="text" ></textarea>
<input type="submit" value="Vložiť" /> 
</form>

<span id="nahlad"> </span>

<script>
   function nahlad()
   {
     var textra = document.getElementById("textra").value;
     alert (textra);
     var xmlhttp;
     if (window.XMLHttpRequest) {xmlhttp=new XMLHttpRequest();
   }
   else
     {xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
   xmlhttp.onreadystatechange=function()
   {
   if (xmlhttp.readyState==4 && xmlhttp.status==200) 
   {
     document.getElementById('nahlad').innerHTML = xmlhttp.responseText;
   }
  }
xmlhttp.open("GET","ssnahlad.php?text=" + textra,true);
xmlhttp.send();
}
</script>

当我在textarea中输入以下内容时(是的,输入中有“新行”)

asd
asd

然后点击Nahlad按钮

ssnahlad.php包含

<?php
$new = $_GET['text'];
echo nl2br($new);
?>

那么为什么带有id = nahlad的范围包含

asdasd 

而不是

asd
asd  

1 个答案:

答案 0 :(得分:1)

既然我在家并且可以测试,这将有效:

  var newText = encodeURIComponent(textra);

在发送之前使用encodeURIComponent将正确呈现您的输出,而无需在服务器端进行解码

相关问题