Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active August 29, 2015 14:21
Show Gist options
  • Save khoand0000/6586222d63ff3089f537 to your computer and use it in GitHub Desktop.
Save khoand0000/6586222d63ff3089f537 to your computer and use it in GitHub Desktop.
submit form by ajax
$(function() { // wait for the DOM to be ready
$('#foo').submit(function() { // bind function to submit event of form
$.ajax({
type: $(this).attr('method'), // get type of request from 'method'
url: $(this).attr('action'), // get url of request from 'action'
data: $(this).serialize(), // serialize the form's data
success: function(response) {
// if everything goes well, update the div with the response
$('#result').html(response);
}
});
return false; // important: prevent the form from submitting
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment