views:

237

answers:

2

I have a button which calls jquery.ajax and submits POST data. I need to get user text from a textarea. So far, no problem. However now i need to set the post data. I have a string in the form as k=v&k2=v2 etc and then i have this user text. I obviously cant write + "&usertext=" + usertext since the text may have code and &kxxx=val which should be inside of the usertext value.

How do i set the ajax data?

+1  A: 

http://api.jquery.com/serialize/

You can call the jQuery method serialize on a form to get a valid data structure to pass in to jQuery.ajax instead of building it from scratch:

$.ajax({
  data: $('#myform').serialize()
});

For the values outside of your textarea you can then add them as hidden inputs in your form and they'll be pulled in to the resulting serialize data.

Cryo
exactly mich that is the way +1
c0mrade
A: 

there are at least 2 options:
1. use http://jquery.malsup.com/form/ jQuery plugin, see 'ajaxSubmit' method
2. use 'serialize' method of jQuery (seee here for details)

zihotki