views:

224

answers:

5

i have one text area

        <textarea name="message" id="message" class="text_field" style="height:200px; width:500px"></textarea>

if i type data in the text area like this

        hello
        this is my test message

        bye
        'abc'

i use following statement to get data from text area

     var message = $('#message').attr('value');

i pass this value to other php file like

      var data = {message:message};
 $.ajax({
 type: "POST",
 url: "show.php",
 data: data,
 etc

when i see data in post value firebug in show exactly i type the data (with new lines & spaces etc) and in php file

          $message=$_POST['message'];

           $Content = file_get_contents($temp_page);
           $Content = str_replace('%%a%%', $message, $Content);

now when i use

             echo $Content

i get all the text in one line not exact i type in text area...

          hello this is my test message bye 'abc'

Thanks

+1  A: 

If you're echoing straight to the browser, try viewing the source of the page. You may find that the browser is interpreting the text as HTML, in which case it will collapse all your whitespace and newlines. Viewing the source should in theory show you what you originally typed, with newlines, extra space etc.

richsage
+2  A: 

That is because browsers don't print newlines outside textareas or other elements that are defined to print the output as is, for example <pre>.

You need to use echo nl2br($Content); to substitute newlines with <br /> elements, which is the equivalent of a newline in (x)HTML.

Tatu Ulmanen
In all my years of PHP coding I have never seen the nl2br command. Thanks! I had been doing this manually.
Benjamin Manns
A: 
Pointy
A: 

IF you right click and click 'View Source' (or some close equivalent), you should see the properly formatted text. This is due to the way that HTML works. Even if you write the following HTML:

hello
this is my test message

bye
'abc'

The output will be inline. You have to write
after each line, or wrap the text in a <pre> tag.

Benjamin Manns
A: 

maybe nl2br($Content)?

stex