cannot store textarea with newline to database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • perhapscwk
    New Member
    • Sep 2007
    • 123

    cannot store textarea with newline to database

    I use below and submit it thru ajax...however, when it store to sql,
    it can;t store the new line. so
    "1
    2"
    become
    "12"

    Code:
    in .php
    <textarea name='note' rows='3' cols='70' id='note' >$in_note
    </textarea>
    
    in .js
    note =document.getElementById("note").value;
    Please help.

    thanks.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    The newline should not be affected by this. How exactly are you passing the value to the database? We would need to see how you: a) create and execute the AJAX call, b) receive and process the AJAX call on the server-side, and c) retrieve and use the value from the database.

    Note that if you are simply printing the value from the database into a HTML document, HTML does not display newline chars. You would either have to display it within <pre> tags or run it through nl2br for HTML to add a new line.

    Comment

    • perhapscwk
      New Member
      • Sep 2007
      • 123

      #3
      ajax as below
      Code:
      function save(str)
      {
      xmlhttp=GetXmlHttpObject();
      if (xmlhttp==null)
        {
        alert ("Browser does not support HTTP Request");
        return;
        }
        
      var url="./getcompany.php";
      url=url+"?q="+str;
      url=url+"&action=save";
      
      savenote =document.getElementById("note").value;
      url=url+"&note="+savenote;
      
      url = url.replace(/#/g, '%23').replace(/"/g, '%27').replace(/!/g, '%21').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/\+/g, '%2B').replace(/~/g, '%7E');
      url=url+"&sid="+Math.random();
      
      
      xmlhttp.onreadystatechange=function(){
      if (xmlhttp.readyState==4)
      {
      id="ajax_getCompany";
      
      document.getElementById(id).innerHTML=xmlhttp.responseText;
      }
      
      }
      
      xmlhttp.open("GET",url,true);
      xmlhttp.send(null);
      }
      php save function

      Code:
      $q=$_GET["q"];
      $action=$_GET["action"];
      
      $note=$_GET["note"];
      
      if ($action=="save") {
      	
      	$strSQL = "update companys 
      	SET xxxx=\"$xxxx\",
      	note=\"$note\",
      	WHERE id = '$q'";
      when i type "1
      2
      3" in textarea, after submit, I check my DB, it store as "123".

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ok, I would guess that the problem is that you are passing the data along the query string. How exactly does the query string that your AJAX query is requesting look like? (Try just alert()'int it before the query is executed.)

        When you pass data along the query string, it should always be URL encoded. In JavaScript you can use the encodeURICompon ent to make sure data is OK to put into the query string.

        You should also consider using a POST request, rather than a GET request. Then you could pass the data in the request, rather than via the URL. See an example of how to do that in this AJAX example. This makes it a lot easier to safely transfer user inputed text.

        Comment

        Working...