1$(function() {
2 $('form.my_form').submit(function(event) {
3 event.preventDefault(); // Prevent the form from submitting via the browser
4 var form = $(this);
5 $.ajax({
6 type: form.attr('method'),
7 url: form.attr('action'),
8 data: form.serialize()
9 }).done(function(data) {
10 // Optionally alert the user of success here...
11 }).fail(function(data) {
12 // Optionally alert the user of an error here...
13 });
14 });
15});
16
1<form id="contactForm1" action="/your_url" method="post">
2 <!-- Form input fields here (do not forget your name attributes). -->
3</form>
4
5<script type="text/javascript">
6 var frm = $('#contactForm1');
7
8 frm.submit(function (e) {
9
10 e.preventDefault();
11
12 $.ajax({
13 type: frm.attr('method'),
14 url: frm.attr('action'),
15 data: frm.serialize(),
16 success: function (data) {
17 console.log('Submission was successful.');
18 console.log(data);
19 },
20 error: function (data) {
21 console.log('An error occurred.');
22 console.log(data);
23 },
24 });
25 });
26</script>
27