Hi all

In a bit of a pickle with AJAX/PHP so i hope osmeone can point out the error of my ways.

I have a textarea and using the onblur event I call an ajax function which ultimately updates my database with the textarea content thus removing the need for a submit button. I have this working great but for some reason my line breaks are being removed from the string by the time it gets to my UPDATE/INSERT query and I can't see why.

Is this a known issue?

In the onblur event of the textarea I am sending the contents to my ajax function by using 'this.value' and when I alert() the value, it has the line breaks which is OK. However, somewhere after this my line breaks are being removed. This is in my ajax function:

	if (xmlNewHttp) {
		// try to connect to the server
		try {
			xmlNewHttp.open("GET", "/ajaxanswers.php?qn="+questionnumber+"&ao="+answeroption+"&st="+sectext+"&sectionid="+sectionid+"", true);
			xmlNewHttp.onreadystatechange = saveQuestionAnswerResponse;
			xmlNewHttp.send(null);


	}
	// display error in case of failure
	catch(e) {
		alert("Cannot connect to the server!");	
	}

}

and in my PHP I have this:

$sectionid = $_REQUEST['sectionid'];
$question = $_REQUEST['qn'];
$answer	= $_REQUEST['ao'];
$usefield = $_REQUEST['st'];

I have tried nl2br around the variable an all sorts - any ideas whats going on!!?

Thanks

    Why do you think the linebreaks are removed?

    Linebreaks do not show in html. You need to use either <pre></pre> around the output, or use nl2br() to display in HTML-context.

    If you show the text, and look at the source of your page, is the formatting still there?

      The line breaks are removed because I am typing them into the text area, viewing the value of the textarea in javascript via alert(); and then when the text is passed through the AJAX GET function to my PHP script it appears all in one line without spaces.

      e.g. I type into my textarea:

      Line One
      Line Two

      This is passed to ajax function which in turn passes it to ajaxanswers.php which has this at the top of the page:

      $answer    = $_REQUEST['ao']; // this gets my text string
      

      And when I echo $answer, it is all on one line as such:

      Line OneLine Two

      Any thoughts?

        Look in the source of your page. It will not be in one lin. Read my post.

        Or view like this:

        echo"
        <pre>$answer
        </pre>";

          I have read your post, and it is in one line.

          When it reaches my $_REQUEST in my PHP page the new lines have been removed.

          Thanks anyway.

            Fixed it by changing the AJAX function to a POST (if anyone cares!)

            See below for the changes:

            			parameters = "qn=1"+questionnumber+"&ao="+answeroption+"&st="+sectext+"&sectionid="+sectionid+"";
            			xmlNewHttp.open("POST", "/ajaxanswers.php", true);
            			xmlNewHttp.onreadystatechange = saveQuestionAnswerResponse;
            			xmlNewHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            			xmlNewHttp.setRequestHeader("Content-length", parameters.length);
            			xmlNewHttp.setRequestHeader("Connection", "close");
            			xmlNewHttp.send(parameters);
            
            
              Write a Reply...