How to submit a form using AJAX
<form action="https://www.formbackend.com/f/acd123" method="post" id="form-signup">
<label>Name</label>
<input type="text" name="name">
<label>Email</label>
<input type="text" name="email">
<button type="submit">Submit</button>
</form><script>
$(function() {
var $form = $('#form-signup');
$form.on("submit", function(e) {
var formUrl = $(this).attr('action');
// We don't want the page to submit and refresh
e.preventDefault();
// POST the request to Formbackend (the formUrl value)
$.ajax({
url: formUrl,
type: 'POST',
headers : {
'accept' : 'application/javascript',
},
// We serialize the form fields
data: $form.serialize()
}).done(function(submissionMessage) {
// Happens when success!
// submissionMessage is the "Submission Text" you set in
// Formbackend - default: "We received your submission"
}).fail(function() {
// Happens when something goes wrong
}).always(function() {
// This always happens
});
});
});
</script>PreviousRedirect to page after submissionNextHow to show your own message when a form has been submitted
Last updated
Was this helpful?