tags:

views:

40

answers:

3

hi

there is a textarea on the page. and i am sending its value via ajax.

var text = $("textarea#text").val();

var dataString = 'text='+ text;


        $.ajax({
      type: "POST",
      url: "do.php?act=save",
      data: dataString,
      cahce: false,
      success: function() {

                   //success


            }

     });

if textarea value is sth like that black & white , it breaks text after the black

if it is sth like that black + white it outputs like black white

how can i avoid this?

thx

+3  A: 

encodeURIComponent

Gabriel McAdams
it worked great, thx
Ahmet vardar
+1  A: 

You need to encode the text, but I think is better to use an Object rather than a String as the data member, jQuery will do the job of properly encoding the POST/GET parameters:

var text = $("textarea#text").val();
var dataObj = {"text": text};
 $.ajax({
   type: "POST",
   url: "do.php?act=save",
   data: dataObj,
   cache: false,
   success: function() {
     //success
   }
 });
CMS
A: 

Or JSON.stringify which converts a JSON object to string representation.

o.k.w