// Function to AJAXify forms
$.fn.AJAXify = function(resultscontainer){
// Check if the element is on the page first
if(this.length){
// Keep track of original element
var og = this;
// Unbind submit event
this.submit(function(e){
// Initialize
e.preventDefault();
var postobj = {};
// Loop through each child element and add to post object
og.find('input, textarea, select, button').each(function(){
var val = ($(this).prop('tagName') == 'TEXTAREA') ? $(this).text() : $(this).val();
var name = $(this).attr('name');
postobj[name] = val;
});
// AJAX request
$.ajax({
url: og.attr('action'),
type: og.attr('method'),
data: postobj
}).done(function(html){
if(resultscontainer){
// Set HTML of resultscontainer if it was passed
resultscontainer.html(html);
}else{
// Otherwise alert
alert(html);
}
});
});
}
}

// Testing it out on our form
$('form').AJAXify();