Skip to content

Instantly share code, notes, and snippets.

@tomwilshere
Created February 8, 2019 11:20
Show Gist options
  • Save tomwilshere/6914efee770b03cc13953ca7baa62d02 to your computer and use it in GitHub Desktop.
Save tomwilshere/6914efee770b03cc13953ca7baa62d02 to your computer and use it in GitHub Desktop.
// 1. When someone submits a form:
jQuery("form").submit(function() {
// 2. Perform an AJAX request ($ is a shortcut for jQuery):
$.post({
// 3. Where to send data: use the URL from the form's action attribute
url: $("form").attr("action"),
// 4. Send the username from the input
data: {
username: $("input[name=username]").val(),
},
// 5. What to do if data submits successfully:
success: function(result){
// 6. Change the paragraph with an id 'message' to display a success message
$("p#message").html("Form Submitted!");
// 7. Hide the form now the user has submitted the form
$("form").hide();
} // END success
}); // END ajax
// 9. Allow form to submit without reloading the page
event.preventDefault();
}) // END submit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment