views:

727

answers:

6

Hello, I'm trying to use Ajax with JQuery, what I want to do is just send multiline textbox value to php with Ajax.

I'm using that code, it sends txtAnswer value to php, unfortunately, it removes new lines (\n) from data. How can I solve this problem... Thanks in advance..

 $.post(
  'post-answer.php',
  {answer: $("#txtAnswer").val(), qid: <?=$question_ID?>},
   function(ajaxResponse){
    $('#answers').html(ajaxResponse)
   }
 );
+3  A: 

are you sure \n are removed? What happend if you put this in your php file:

<?php
print_r($_REQUEST['answer']);
?>

using firebug you can see the output and be sure if \n are there or not.

I never hear about jquery or prototype removing \n

remember that if you want to show new lines on html format you need convert them to <br/>. Which I think you are trying to do.

You can use nr2br to do this.

Gabriel Sosa
A: 

Hi!

Actually i' ve the same problem. If i' m trying to send the content of a textarea through ajax, '\n' is removed. For example if i try to send

line1line2 , then, in the log file i can see only save.php?text=line1line2 , so '\n' is removed. I didn' t find the solution yet. Anyone found it?

Are you sending through POST or GET?
TRiG
A: 

Hi all!

I'm also still looking for the answer.

Burak
+2  A: 

I've also encountered this problem. I have a div updating a preview as a user types in a textarea. Here is what I found to work in my situation:

 jQuery('#task_description').keyup(function() {
  jQuery('#pre_description').html(jQuery('#task_description').val().replace( /\n/g, '<br \\>' ));
 }
Heather Brysiewicz
A: 

I'm using .ajax with type:POST and it's saving the new lines for me

justinl
Actually never mind, once I got the type of .ajax request set to POST, it fixed this (using GET does lose the new lines).
justinl
A: 

Try: escape( $("#txtAnswer").val() )

=-)

jevel