Created
September 23, 2016 02:31
-
-
Save yagamicoder/34ca65ce031b2b38340e33b943aeb9f8 to your computer and use it in GitHub Desktop.
Common js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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