The jQuery form plugin offers the ease of use of ajax with forms. He will use the attributes of the form to determine how and where to submit the form. The form method attribute tells the plugin to use the request type. The action attribute of the form tells where the form is submitted.
Given an example of this form:
<form id="myform" action="submit.php" method="post"> </form>
In essence, this allows you to write:
$('#myform').ajaxForm();
instead
$.post("submit.php", $("#myform").serialize());
The jquery Form plugin will also allow you to submit code through Ajax.
$('#myform').ajaxSubmit();
The jQuery Forms plugin will serialize the form anyway, and you will have to serialize before submitting it to the server. The jQuery Form plugin simply serializes the form behind the scenes. The above examples do not process any responses from the server.
Using the code below, you can add an answer to elements whose class attribute contains a βresultβ.
$('#myform').ajaxForm({ target : ".result"});
You can use any selector that is supported in jQuery for the target option.
I'm not sure if you can use deferred methods or not. I doubt it because compatibility is for 1.3.2+, and the ajax built-in methods are wrapped in a plugin. So the Deferred object never returns from the plug-in's internal modules.
The jQuery form has a beforeSubmit parameter that you can use. So, adding to the above code, we get:
$('#myform').ajaxForm({ target: ".result", beforeSubmit: function(arr, $form, options) { // arr: The array of form data // The array of form data takes the following form: // [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] //some code to check if we already submitted the form // return false to cancel submit } });
However, I would recommend not to do this if the user has finished the page after submitting the form. This would add unnecessary complexity to your client code. It should be nice if the user will do other things on the page after submitting the form. The jQuery Forms plugin has a successful option, which is called by a function call if the server returns a 200 "OK" response. You can also check the authors website for the plugin http://jquery.malsup.com/form/ for more information about the plugin.