ajaxForm
and
ajaxSubmit
, gather information from the form element to determine how to manage the submit process. Both of these methods support numerous options which allows you to have full control over how the data is submitted. Submitting a form with AJAX doesn't get any easier than this!
<form id="myForm" action="comment.php" method="post"> Name: <input type="text" name="name" /> Comment: <textarea name="comment"></textarea> <input type="submit" value="Submit Comment" /> </form>
<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.github.com/jquery.form.js"></script> <script> // wait for the DOM to be loaded $(document).ready(function() { // bind 'myForm' and provide a simple callback function $('#myForm').ajaxForm(function() { alert("Thank you for your comment!"); }); }); </script> </head> ...
That's it!javascript
When this form is submitted the name and comment fields will be posted to comment.php. If the server returns a success status then the user will see a "Thank you" message.php
ajaxForm
ajaxForm
in your document's
ready
function to prepare your form(s) for AJAX submission.
ajaxForm
takes zero or one argument. The single argument can be either a callback function or an
Options Object.
Note: You can pass any of the standard $.ajax
options to ajaxFormhtml
Example:java
$('#myFormId').ajaxForm();
ajaxSubmit
ajaxSubmit
takes zero or one argument. The single argument can be either a callback function or an
Options Object.
Note: You can pass any of the standard $.ajax
options to ajaxSubmitjquery
Example:git
// attach handler to form's submit event $('#myFormId').submit(function() { // submit the form $(this).ajaxSubmit(); // return false to prevent normal browser submit and page navigation return false; });
formSerialize
name1=value1&name2=value2
Example:github
var queryString = $('#myFormId').formSerialize(); // the data could now be submitted using $.get, $.post, $.ajax, etc $.post('myscript.php', queryString);
fieldSerialize
name1=value1&name2=value2
Example:ajax
var queryString = $('#myFormId .specialFields').fieldSerialize();
fieldValue
Example:json
// get the value of the password input var value = $('#myFormId :password').fieldValue(); alert('The password is: ' + value[0]);
resetForm
Example:api
$('#myFormId').resetForm();
clearForm
$('#myFormId').clearForm();
clearFields
$('#myFormId .specialFields').clearFields();
Note: Aside from the options listed below, you can also pass any of the standard $.ajax
options to ajaxForm and ajaxSubmit.
ajaxForm
and
ajaxSubmit
support numerous options which can be provided using an Options Object. The Options Object is simply a JavaScript object that contains properties with values set as follows:
Example:
// prepare Options Object var options = { target: '#divToUpdate', url: 'comment.php', success: function() { alert('Thanks for your comment!'); } }; // pass options to ajaxForm $('#myForm').ajaxForm(options);
Note that the Options Object can also be used to pass values to jQuery's $.ajax
method. If you are familiar with the options supported by $.ajax
you may use them in the Options Object passed to ajaxForm
and ajaxSubmit
.
The following code controls the HTML form beneath it. It uses ajaxForm
to bind the form and demonstrates how to use pre- and post-submit callbacks.
// prepare the form when the DOM is ready $(document).ready(function() { var options = { target: '#output1', // target element(s) to be updated with server response beforeSubmit: showRequest, // pre-submit callback success: showResponse // post-submit callback // other available options: //url: url // override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute //dataType: null // 'xml', 'script', or 'json' (expected server response type) //clearForm: true // clear all form fields after successful submit //resetForm: true // reset the form after successful submit // $.ajax options can be used here too, for example: //timeout: 3000 }; // bind form using 'ajaxForm' $('#myForm1').ajaxForm(options); }); // pre-submit callback function showRequest(formData, jqForm, options) { // formData is an array; here we use $.param to convert it to a string to display it // but the form plugin does this for you automatically when it submits the data var queryString = $.param(formData); // jqForm is a jQuery object encapsulating the form element. To access the // DOM element for the form do this: // var formElement = jqForm[0]; alert('About to submit: \n\n' + queryString); // here we could return false to prevent the form from being submitted; // returning anything other than false will allow the form submit to continue return true; } // post-submit callback function showResponse(responseText, statusText, xhr, $form) { // for normal html responses, the first argument to the success callback // is the XMLHttpRequest object's responseText property // if the ajaxForm method was passed an Options Object with the dataType // property set to 'xml' then the first argument to the success callback // is the XMLHttpRequest object's responseXML property // if the ajaxForm method was passed an Options Object with the dataType // property set to 'json' then the first argument to the success callback // is the json data object returned by the server alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + '\n\nThe output div should have already been updated with the responseText.'); }