I have a Django ModelForm but don't want the user to click on a reguar submit button. Instead, I made my own submit button and will post the form data using Ajax/jQuery. The data to post is a mix of the form data plus other information I gather on my UI. But... how do i get the formatted POST data the html form was supposed to submit? If I can get it like JSON or jQuery object, better!
+1
A:
You can use jQuery.serialize()
for this which you in turn can pass as 2nd argument of jQuery.post()
.
$('#formid').submit(function() {
$.post('serverUrl', $(this).serialize(), callbackFunction);
return false;
});
Alternatively, you can also use the jQuery Form plugin for this, which makes stuff easier.
BalusC
2010-07-08 22:59:51
Good! But I need to mix this serialized with other object like {param1:1, param2:2}Another problem is that when I create a <form> object, whenever I call $.post(), it will actually post my form, reloading the page, which is no good!
Roger
2010-07-09 00:31:47
1. Well you can get `$(this).serialize()` into a javascript object before the post call, and add fields manually to it; 2. Really, no it doesn't. `$.post` is an AJAX submit, and `return false` means that the form's default submit action is not carried out.
Daniel Roseman
2010-07-09 08:14:23
@Roger, @Daniel is right. Do something like `var data = $(this).serialize(); data.param1 = 1;`. The `return false;` should block form's default action.
BalusC
2010-07-09 10:48:22
I was missing the `return false;`, not it won't submit! I'm posting something like this... `$.param(myparams) + "`. Thanks!!
Roger
2010-07-09 15:53:43