Skip to content

Instantly share code, notes, and snippets.

@Edwin-Luijten
Created May 8, 2012 10:31
Show Gist options
  • Save Edwin-Luijten/2634115 to your computer and use it in GitHub Desktop.
Save Edwin-Luijten/2634115 to your computer and use it in GitHub Desktop.
JS/JQUERY : A simple function to create an ajax form
$.fn.ajaxForm = function(opts) {
var defaults = {
//Default values for options
ajaxType : 'post',
ajaxCache : false
}
// extend the options from defaults with user's options
var options = $.extend(defaults, opts || {});
return this.each(function(){ // jQuery chainability
$(this).submit(function(e){
e.preventDefault();
var form = this, url = $(this).attr("action");
$.ajax({
type: options.ajaxType,
url: url,
cache: options.ajaxCache,
data: $(this).serialize()
}).done(function(data){
$(form)[0].reset(); // Reset form after it is send
$(form).find(options.responseContainer).html(data);
});
return false;
});
});
}
$('#your-form-id').ajaxForm({
responseContainer : '.your-form-response-container' //May be a class(.) or id(#)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment