Skip to content

Instantly share code, notes, and snippets.

@umkasanki
Created March 22, 2019 07:50
Show Gist options
  • Save umkasanki/515012529b76f30054d48c5c02062777 to your computer and use it in GitHub Desktop.
Save umkasanki/515012529b76f30054d48c5c02062777 to your computer and use it in GitHub Desktop.
$('#js-contact-form').submit(function(ev) {
// Prevent the form from actually submitting
ev.preventDefault();
// Send it to the server
$.post({
url: '/',
dataType: 'json',
data: $(this).serialize(),
success: function(response) {
if (response.success) {
$('#js-form-header').hide();
$('#js-form-thanks').show();
$('input, textarea').val('');
} else {
// response.error will be an object containing any validation errors that occurred, indexed by field name
// e.g. response.error.fromName => ['From Name is required']
alert('An error occurred. Please try again.');
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment