title: javascript/html/php capture linebreaks in textarea
body: <p>I am desperately trying to capture the line breaks in a textarea.  I want to store the contents of the textarea in a database and then display it in a textarea when pulled from the database.  The textarea should serve as an editing box.  At no point do I need to display in html.</p>
  
  <p>I was able to get this to work on a normal html page that posts the textarea content to a server by converting the text from the textarea into an array of lines, exploding on the characters \r\n, and imploding it into a comma delimited list of lines. Then when I want to redisplay the lines, I replace the commas with \n and get text with line breaks that I can display inside a textarea.  A bit of a hack but it works.</p>
  
  <p>However, it does not seem to work when instead of posting the textarea contents to the server using html, I pull them using getElementByID in javascript and then post to the server in javascript.</p>
  
  <p>When I get the textarea contents using html and explode on \r\n I get an array with as many elements as lines.</p>
  
  <p>However, when I pull the textarea contents using getElementByID in javascript, and explode on \r\n, I just get one element in the array where the lines run on.  In other words, I do not get the linebreaks</p>
  
  <p>Anyone know why the linebreaks would disappear when pulling the text in javascript and have any suggestions on how to preserve line breaks in javascript?</p>
  
  <p>Thanks.</p>
  
  <p>Abbreviated code:
  javascript</p>
  
  <pre><code>getText() {
  
  var textobj = document.getElementById("text");
  var text = textobj.value;
  
  ...some code for AJAX
  
  
      document.getElementById("droptext").innerHTML=xmlhttp.responseText;
  ... some more code for Ajax
  
  xmlhttp.open("GET","savetext.php?text="+text,true);
  xmlhttp.send();
  }
  </code></pre>
  
  <p>html</p>
  
  <pre><code>&lt;div id="droptext"&gt;&lt;textarea id="text"&gt;&lt;textarea&gt;&lt;/div&gt;
  &lt;a href="javascript:void(0);" onclick="getText();"&gt;Save Text&lt;/a&gt;
  </code></pre>
  
  <p>php</p>
  
  <pre><code>$text = $_GET['text'];
  $textarray = explode("\r\n", $text);
  $text = implode(",",$textarray); //converts to comma delimited list
  
  ...store in database
  
  ...fetch from database and echo text back for editing
  
  $text = $row['text'];
  $text = str_replace( ",", "\n", $text);
  
  echo '&lt;textarea&gt;'.$text.'&lt;/textarea&gt;';
  </code></pre>