I have the following Bootstrap contact form:
<form id="contactForm" class="form-horizontal" data-async data-target="#contact" action="/contact.php" method="POST">
(...)
<button class="btn btn-primary pull-right" type="submit">Send</button>
</form>
And this jQuery code:
$('form[data-async]').on('submit', function(event) {
var $form = $(this);
var $target = $($form.attr('data-target'));
$.ajax({
type: $form.attr('method'),
url: $form.attr('action'),
data: $form.serialize(),
success: function(data, status) {
$target.html(data);
}
});
event.preventDefault();
});
I can submit the form and get an AJAX response once but the second time I try to do it it loads contact.php
in the navigator. Which I don't get because event.preventDefault()
should prevent this. How can I make multiple AJAX calls work?