| 4 | | * All arguments are optional. |
| 5 | | * |
| 6 | | * If a target arg is provided, it is used to identify the element(s) to be updated with the |
| 7 | | * server response. The target argument can be a jQuery selector string, a jQuery object, or a DOM element. |
| 8 | | * |
| 9 | | * If a post-submit callback method is provided it is invoked after the response has been returned |
| 10 | | * from the server. |
| 11 | | * |
| 12 | | * If neither target or post-submit arguments are provided then the data returned from the server (if any) |
| 13 | | * is evaluated in the global context. |
| 14 | | * |
| 15 | | * The pre-submit callback can be provided as a hook for running pre-submit logic or for validating the |
| 16 | | * form data. If the pre-submit callback returns false the form will not be submitted. The pre-submit callback |
| 17 | | * is invoked with two arguments, the form data in array format, and the jQuery object. The form |
| 18 | | * data array takes the form: |
| | 4 | * Options are provided via an options object. The following options are supported: |
| | 5 | * |
| | 6 | * target: Identifies the element(s) in the page to be updated with the server response. |
| | 7 | * This value may be specified as a jQuery selection string, a jQuery object, |
| | 8 | * or a DOM element. |
| | 9 | * default value: null |
| | 10 | * |
| | 11 | * url: URL to which the form data will be submitted. |
| | 12 | * default value: value of form's 'action' attribute |
| | 13 | * |
| | 14 | * method: The method in which the form data should be submitted, 'GET' or 'POST'. |
| | 15 | * default value: value of form's 'method' attribute (or 'GET' if none found) |
| | 16 | * |
| | 17 | * before: Callback method to be invoked before the form is submitted. |
| | 18 | * default value: null |
| | 19 | * |
| | 20 | * after: Callback method to be invoked after the form has been successfully submitted. |
| | 21 | * default value: null |
| | 22 | * |
| | 23 | * dataType: Expected dataType of the response. One of: null, 'xml', 'script', or 'json' |
| | 24 | * default value: null |
| | 25 | * |
| | 26 | * semantic: Boolean flag indicating whether data must be submitted in semantic order (slower). |
| | 27 | * default value: false |
| | 28 | * |
| | 29 | * |
| | 30 | * The 'before' callback can be provided as a hook for running pre-submit logic or for |
| | 31 | * validating the form data. If the 'before' callback returns false then the form will |
| | 32 | * not be submitted. The 'before' callback is invoked with two arguments: the form data |
| | 33 | * in array format, and the jQuery object. The form data array takes the following form: |
| 22 | | * |
| 23 | | * The url and mth arguments can also be provided to override the form defaults. If not provided, |
| 24 | | * the form emement's 'action' and 'method' properties will be used. |
| 25 | | * |
| 26 | | * The semantic argument can be used to force form serialization in semantic order. If your form must |
| 27 | | * be submitted with name/value pairs in semantic order then pass true for this arg, otherwise pass false |
| 28 | | * (or nothing) to avoid the overhead for this logic (which can be significant for very large forms). |
| 29 | | * |
| 30 | | * @example $("#form-id").ajaxSubmit("#destination"); |
| 31 | | * @desc The form is submitted and the resulting HTML contents are loaded into the #destination element. |
| 32 | | * |
| 33 | | * @example $("#form-id").ajaxSubmit(function(){ |
| 34 | | * alert("all done!"); |
| | 37 | * If an 'after' callback method is provided it is invoked after the response has been returned |
| | 38 | * from the server. It is passed the responseText or responseXML value (depending on dataType). |
| | 39 | * See jQuery.ajax for further details. |
| | 40 | * |
| | 41 | * The dataType option provides a means for specifying how the server response should be handled. |
| | 42 | * This maps directly to the jQuery.httpData method. The following values are supported as of |
| | 43 | * jQuery verions 1.0.2: |
| | 44 | * 'xml': if dataType == 'xml' the server response is treated as XML and the 'after' |
| | 45 | * callback method, if specified, will be passed the responseXML value |
| | 46 | * 'json': if dataType == 'json' the server response will be evaluted and passed to |
| | 47 | * the 'after' callback, if specified |
| | 48 | * 'script': if dataType == 'script' the server response is evaluated in the global context |
| | 49 | * |
| | 50 | * Note that it does not make sense to use both the 'target' and 'dataType' options. If both |
| | 51 | * are provided the target will be ignored. |
| | 52 | * |
| | 53 | * The semantic argument can be used to force form serialization in semantic order. If your |
| | 54 | * form must be submitted with name/value pairs in semantic order then pass true for this arg, |
| | 55 | * otherwise pass false (or nothing) to avoid the overhead for this logic (which can be |
| | 56 | * significant for very large forms). |
| | 57 | * |
| | 58 | * When used on its own, ajaxSubmit() is typically bound to a form's submit event like this: |
| | 59 | * |
| | 60 | * $("#form-id").submit(function() { |
| | 61 | * $(this).ajaxSubmit(options); |
| | 62 | * return false; // cancel conventional submit |
| 36 | | * @desc The form is submitted and a callback is fired, letting you know when it's done. |
| 37 | | * |
| 38 | | * @example $("#form-id").ajaxSubmit(); |
| 39 | | * @desc The form is submitted and the results returned from the server are |
| 40 | | * automatically executed (useful for having the server return more Javascript commands to execute). |
| 41 | | * |
| 42 | | * @example $("#form-id").submit(function() { |
| 43 | | * $(this).ajaxSubmit("#destination"); |
| 44 | | * return false; |
| | 64 | * |
| | 65 | * When using ajaxForm(), however, this is done for you. |
| | 66 | * |
| | 67 | * |
| | 68 | * @example |
| | 69 | * var options = { |
| | 70 | * target: '#myTargetDiv' |
| | 71 | * }; |
| | 72 | * $('#myForm').ajaxSubmit(options); |
| | 73 | * @desc Submit form and update page element with server response |
| | 74 | * |
| | 75 | * |
| | 76 | * @example |
| | 77 | * var options = { |
| | 78 | * after: function(responseText) { |
| | 79 | * alert(responseText); |
| | 80 | * } |
| | 81 | * }; |
| | 82 | * $('#myForm').ajaxSubmit(options); |
| | 83 | * @desc Submit form and alert the server response |
| | 84 | * |
| | 85 | * |
| | 86 | * @example |
| | 87 | * var options = { |
| | 88 | * before: function(formArray, jqForm) { |
| | 89 | * if (formArray.length == 0) { |
| | 90 | * alert('Please enter data.'); |
| | 91 | * return false; |
| | 92 | * } |
| | 93 | * } |
| | 94 | * }; |
| | 95 | * $('#myForm').ajaxSubmit(options); |
| | 96 | * @desc Pre-submit validation which aborts the submit operation if form data is empty |
| | 97 | * |
| | 98 | * |
| | 99 | * @example |
| | 100 | * var options = { |
| | 101 | * url: myJsonUrl.php, |
| | 102 | * dataType: 'json', |
| | 103 | * after: function(data) { |
| | 104 | * // 'data' is an object representing the the evaluated json data |
| | 105 | * } |
| | 106 | * }; |
| | 107 | * $('#myForm').ajaxSubmit(options); |
| | 108 | * @desc json data returned and evaluated |
| | 109 | * |
| | 110 | * |
| | 111 | * @example |
| | 112 | * var options = { |
| | 113 | * url: myXmlUrl.php, |
| | 114 | * dataType: 'xml', |
| | 115 | * after: function(responseXML) { |
| | 116 | * // responseXML is XML document object |
| | 117 | * var data = $('myElement', responseXML).text(); |
| | 118 | * } |
| | 119 | * }; |
| | 120 | * $('#myForm').ajaxSubmit(options); |
| | 121 | * @desc XML data returned from server |
| | 122 | * |
| | 123 | * |
| | 124 | * @example |
| | 125 | * $('#myForm).submit(function() { |
| | 126 | * $(this).ajaxSubmit(); |
| | 127 | * return false; |
| 72 | | var get = mth == 'GET'; |
| 73 | | |
| 74 | | if (get) url = url + '?' + q; |
| 75 | | |
| 76 | | // if no target or 'post' callback was provided then default to a callback |
| 77 | | // that evals the response |
| 78 | | var t = target || post_cb || function(r) { |
| 79 | | if (r.responseText) eval.call(window, r.responseText) |
| 80 | | }; |
| 81 | | |
| 82 | | if (t && t.constructor != Function) |
| 83 | | jQuery(t).load(url, get ? null : a, post_cb); |
| | 159 | var get = (options.method && options.method.toUpperCase() == 'GET'); |
| | 160 | |
| | 161 | if (get) |
| | 162 | // if url already has a '?' then append args after '&' |
| | 163 | options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; |
| | 164 | |
| | 165 | // perform a load on the target only if dataType is not provided |
| | 166 | if (!options.dataType && options.target) |
| | 167 | jQuery(options.target).load(options.url, get ? null : a, options.after); |
| 102 | | * All arguments are optional. |
| 103 | | * |
| 104 | | * If a target arg is provided, it is used to identify the element(s) to be updated with the |
| 105 | | * server response. The target argument can be a jQuery selector string, a jQuery object, or a DOM element. |
| 106 | | * |
| 107 | | * If a post-submit callback method is provided it is invoked after the response has been returned |
| 108 | | * from the server. |
| 109 | | * |
| 110 | | * If neither target or post-submit arguments are provided then the data returned from the server (if any) |
| 111 | | * is evaluated in the global context. |
| 112 | | * |
| 113 | | * The pre-submit callback can be provided as a hook for running pre-submit logic or for validating the |
| 114 | | * form data. If the pre-submit callback returns false the form will not be submitted. The pre-submit callback |
| 115 | | * is invoked with two arguments, 1) the form data in array format, and 2) the jQuery object. The form |
| 116 | | * data array takes the form: |
| 117 | | * |
| 118 | | * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] |
| 119 | | * |
| 120 | | * |
| 121 | | * The semantic argument can be used to force form serialization in semantic order. If your form must |
| 122 | | * be submitted with name/value pairs in semantic order then pass true for this arg, otherwise pass false |
| 123 | | * (or nothing) to avoid the overhead for this logic (which can be significant for very large forms). |
| 124 | | * |
| 125 | | * @example $('#form-id').ajaxForm(); |
| 126 | | * @desc Just eval the results returned from the backend. |
| 127 | | * |
| 128 | | * @example $('#form-id').ajaxForm('#target-id'); |
| 129 | | * @desc Render backend results directly to target ID (expects (x)HTML). |
| 130 | | * |
| 131 | | * @example $('#form-id').ajaxForm(post_callback); |
| 132 | | * @desc Submit to backend URL (form action) then call this function. |
| 133 | | * |
| 134 | | * @example $('#form-id').ajaxForm('#target-id', post_callback); |
| 135 | | * @desc Load target ID with backend results then call a function. |
| 136 | | * |
| 137 | | * @example $('#form-id').ajaxForm('#target-id', null, pre_callback); |
| 138 | | * @desc Call a browser function (for validation) and then load server results to target ID. |
| 139 | | * |
| 140 | | * @example $('#form-id').ajaxForm('#target-id', post_callback, pre_callback); |
| 141 | | * @desc Call validation function first then load server results to target ID and then call post_callback function. |
| 142 | | * |
| 143 | | * @name ajaxForm |
| 144 | | * @param target jQuery selector string, jQuery object or DOM element which identifies the element(s) to update with the server response |
| 145 | | * @param post_cb callback to be invoked after any results are returned |
| 146 | | * @param pre_cb callback to be invoked after gathering the form data but before submitting the form to the server |
| 147 | | * @param semantic true if serialization must maintain strict semantic ordering of elements (slower) |
| | 194 | * The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely |
| | 195 | * passes the options argument along after properly binding events for submit elements and |
| | 196 | * the form itself. See ajaxSubmit for a full description of the options argument. |
| | 197 | * |
| | 198 | * |
| | 199 | * @example |
| | 200 | * var options = { |
| | 201 | * target: '#myTargetDiv' |
| | 202 | * }; |
| | 203 | * $('#myForm').ajaxSForm(options); |
| | 204 | * @desc Bind form's submit event so that 'myTargetDiv' is updated with the server response |
| | 205 | * when the form is submitted. |
| | 206 | * |
| | 207 | * |
| | 208 | * @example |
| | 209 | * var options = { |
| | 210 | * after: function(responseText) { |
| | 211 | * alert(responseText); |
| | 212 | * } |
| | 213 | * }; |
| | 214 | * $('#myForm').ajaxSubmit(options); |
| | 215 | * @desc Bind form's submit event so that server response is alerted after the form is submitted. |
| | 216 | * |
| | 217 | * |
| | 218 | * @example |
| | 219 | * var options = { |
| | 220 | * before: function(formArray, jqForm) { |
| | 221 | * if (formArray.length == 0) { |
| | 222 | * alert('Please enter data.'); |
| | 223 | * return false; |
| | 224 | * } |
| | 225 | * } |
| | 226 | * }; |
| | 227 | * $('#myForm').ajaxSubmit(options); |
| | 228 | * @desc Bind form's submit event so that pre-submit callback is invoked before the form |
| | 229 | * is submitted. |
| | 230 | * |
| | 231 | * |
| | 232 | * @name ajaxForm |
| | 233 | * @param options object literal containing options which control the form submission process |