Skip to content

Instantly share code, notes, and snippets.

@yagamicoder
Created September 23, 2016 02:31
Show Gist options
  • Save yagamicoder/34ca65ce031b2b38340e33b943aeb9f8 to your computer and use it in GitHub Desktop.
Save yagamicoder/34ca65ce031b2b38340e33b943aeb9f8 to your computer and use it in GitHub Desktop.
Common js
$(document).ready(function(){
//Ajax form validation
$("#ajax-form").submit(function() {
var formData = $(this).serialize();
$.ajax({
type: "POST",
url: "ajax-form.php",
data: formData,
dataType: 'json',
cache: false,
success: function(data) {
//If the data key from PHP json array is "error"
if (data.error) {
$("#error").hide().stop(true, true).slideDown(400).html(data.error);
$("#success").hide();
} else {
$("#error").hide();
//Reset the form
$("#ajax-form")[0].reset();
$("#success").slideDown(400).html(data.success);
//Fade out success message after 7 secs
setTimeout(function(){
$('#success').fadeOut(600);
}, 7000);
}
},
error: function() {
alert("Server error. Please try again later.");
}
});
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment