Skip to content

Instantly share code, notes, and snippets.

@carolinecodes
Last active December 16, 2020 04:21
Show Gist options
  • Save carolinecodes/74001a99c69a0b0e0917b2cdab80d203 to your computer and use it in GitHub Desktop.
Save carolinecodes/74001a99c69a0b0e0917b2cdab80d203 to your computer and use it in GitHub Desktop.
Sample of how to POST form data from a static HTML page using AJAX. Also includes HTML-friendly notifications that can be populated on the page.
$("#submit-button-id").submit(function( event ){
event.preventDefault();
var form = $(this);
$.ajax({
type: 'POST',
url: 'https://url_here',
data: form.serialize(),
dataType: 'json',
success: function() {
form.hide();
$("#your-element-id-here").html("Message here: success. Supports styling/HTML.");
},
error: function(response,err) {
if (response.status==0) {
$("#your-element-id-here").html("Message here");
} else if(response.status==404) {
$("#your-element-id-here").html("Message here: 400 Error");
} else if(response.status==500) {
$("#your-element-id-here").html("Message here: 500 Error");
} else if(err=='parsererror') {
$("#your-element-id-here").html("Message here: Parse Error");
} else if(err=='timeout'){
$("#your-element-id-here").html("Message here: Timeout");
} else {
$("#your-element-id-here").html("Message here: Unknown error");
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment