简体   繁体   中英

Textarea - missing new line

I have following code:

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

When I enter the following into the textarea (yes there is "new line" with enter)

asd
asd

and click the Nahlad button

ssnahlad.php contains

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

so why does the span with id=nahlad contain

asdasd 

instead of

asd
asd  

Now that I'm at home and can test, this will work:

  var newText = encodeURIComponent(textra);

using encodeURIComponent before you send it will correctly render your output, with no decoding on the server side

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM