/* * jquery form plugin * @requires jquery v1.1 or later * * examples at: http://malsup.com/jquery/form/ * dual licensed under the mit and gpl licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * revision: $id: jquery.form.js 3028 2007-08-31 13:37:36z joern.zaefferer $ */ (function($) { /** * ajaxsubmit() provides a mechanism for submitting an html form using ajax. * * ajaxsubmit accepts a single argument which can be either a success callback function * or an options object. if a function is provided it will be invoked upon successful * completion of the submit and will be passed the response from the server. * if an options object is provided, the following attributes are supported: * * target: identifies the element(s) in the page to be updated with the server response. * this value may be specified as a jquery selection string, a jquery object, * or a dom element. * default value: null * * url: url to which the form data will be submitted. * default value: value of form's 'action' attribute * * type: the method in which the form data should be submitted, 'get' or 'post'. * default value: value of form's 'method' attribute (or 'get' if none found) * * data: additional data to add to the request, specified as key/value pairs (see $.ajax). * * beforesubmit: callback method to be invoked before the form is submitted. * default value: null * * success: callback method to be invoked after the form has been successfully submitted * and the response has been returned from the server * default value: null * * datatype: expected datatype of the response. one of: null, 'xml', 'script', or 'json' * default value: null * * semantic: boolean flag indicating whether data must be submitted in semantic order (slower). * default value: false * * resetform: boolean flag indicating whether the form should be reset if the submit is successful * * clearform: boolean flag indicating whether the form should be cleared if the submit is successful * * * the 'beforesubmit' callback can be provided as a hook for running pre-submit logic or for * validating the form data. if the 'beforesubmit' callback returns false then the form will * not be submitted. the 'beforesubmit' callback is invoked with three arguments: the form data * in array format, the jquery object, and the options object passed into ajaxsubmit. * the form data array takes the following form: * * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] * * if a 'success' callback method is provided it is invoked after the response has been returned * from the server. it is passed the responsetext or responsexml value (depending on datatype). * see jquery.ajax for further details. * * * the datatype option provides a means for specifying how the server response should be handled. * this maps directly to the jquery.httpdata method. the following values are supported: * * 'xml': if datatype == 'xml' the server response is treated as xml and the 'success' * callback method, if specified, will be passed the responsexml value * 'json': if datatype == 'json' the server response will be evaluted and passed to * the 'success' callback, if specified * 'script': if datatype == 'script' the server response is evaluated in the global context * * * note that it does not make sense to use both the 'target' and 'datatype' options. if both * are provided the target will be ignored. * * the semantic argument can be used to force form serialization in semantic order. * this is normally true anyway, unless the form contains input elements of type='image'. * if your form must be submitted with name/value pairs in semantic order and your form * contains an input of type='image" then pass true for this arg, otherwise pass false * (or nothing) to avoid the overhead for this logic. * * * when used on its own, ajaxsubmit() is typically bound to a form's submit event like this: * * $("#form-id").submit(function() { * $(this).ajaxsubmit(options); * return false; // cancel conventional submit * }); * * when using ajaxform(), however, this is done for you. * * @example * $('#myform').ajaxsubmit(function(data) { * alert('form submit succeeded! server returned: ' + data); * }); * @desc submit form and alert server response * * * @example * var options = { * target: '#mytargetdiv' * }; * $('#myform').ajaxsubmit(options); * @desc submit form and update page element with server response * * * @example * var options = { * success: function(responsetext) { * alert(responsetext); * } * }; * $('#myform').ajaxsubmit(options); * @desc submit form and alert the server response * * * @example * var options = { * beforesubmit: function(formarray, jqform) { * if (formarray.length == 0) { * alert('please enter data.'); * return false; * } * } * }; * $('#myform').ajaxsubmit(options); * @desc pre-submit validation which aborts the submit operation if form data is empty * * * @example * var options = { * url: myjsonurl.php, * datatype: 'json', * success: function(data) { * // 'data' is an object representing the the evaluated json data * } * }; * $('#myform').ajaxsubmit(options); * @desc json data returned and evaluated * * * @example * var options = { * url: myxmlurl.php, * datatype: 'xml', * success: function(responsexml) { * // responsexml is xml document object * var data = $('myelement', responsexml).text(); * } * }; * $('#myform').ajaxsubmit(options); * @desc xml data returned from server * * * @example * var options = { * resetform: true * }; * $('#myform').ajaxsubmit(options); * @desc submit form and reset it if successful * * @example * $('#myform).submit(function() { * $(this).ajaxsubmit(); * return false; * }); * @desc bind form's submit event to use ajaxsubmit * * * @name ajaxsubmit * @type jquery * @param options object literal containing options which control the form submission process * @cat plugins/form * @return jquery */ $.fn.ajaxsubmit = function(options) { if (typeof options == 'function') options = { success: options }; options = $.extend({ url: this.attr('action') || window.location, type: this.attr('method') || 'get' }, options || {}); // hook for manipulating the form data before it is extracted; // convenient for use with rich editors like tinymce or fckeditor var veto = {}; $.event.trigger('form.pre.serialize', [this, options, veto]); if (veto.veto) return this; var a = this.formtoarray(options.semantic); if (options.data) { for (var n in options.data) a.push( { name: n, value: options.data[n] } ); } // give pre-submit callback an opportunity to abort the submit if (options.beforesubmit && options.beforesubmit(a, this, options) === false) return this; // fire vetoable 'validate' event $.event.trigger('form.submit.validate', [a, this, options, veto]); if (veto.veto) return this; var q = $.param(a);//.replace(/%20/g,'+'); if (options.type.touppercase() == 'get') { options.url += (options.url.indexof('?') >= 0 ? '&' : '?') + q; options.data = null; // data is null for 'get' } else options.data = q; // data is the query string for 'post' var $form = this, callbacks = []; if (options.resetform) callbacks.push(function() { $form.resetform(); }); if (options.clearform) callbacks.push(function() { $form.clearform(); }); // perform a load on the target only if datatype is not provided if (!options.datatype && options.target) { var oldsuccess = options.success || function(){}; callbacks.push(function(data) { if (this.evalscripts) $(options.target).attr("innerhtml", data).evalscripts().each(oldsuccess, arguments); else // jquery v1.1.4 $(options.target).html(data).each(oldsuccess, arguments); }); } else if (options.success) callbacks.push(options.success); options.success = function(data, status) { for (var i=0, max=callbacks.length; i < max; i++) callbacks[i](data, status, $form); }; // are there files to upload? var files = $('input:file', this).fieldvalue(); var found = false; for (var j=0; j < files.length; j++) if (files[j]) found = true; if (options.iframe || found) // options.iframe allows user to force iframe mode fileupload(); else $.ajax(options); // fire 'notify' event $.event.trigger('form.submit.notify', [this, options]); return this; // private function for handling file uploads (hat tip to yahoo!) function fileupload() { var form = $form[0]; var opts = $.extend({}, $.ajaxsettings, options); var id = 'jqformio' + $.fn.ajaxsubmit.counter++; var $io = $('