Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery AJAX form submits twice

I've got a form that I'm capturing via jQuery and sending via AJAX. My problem is that the form submits more than once every time I refresh the page.

I've tried to unbind the submit button but then the form is posted normally after the first submit. Any help would be appreciated

$('#exportForm').submit(function() {   $.ajax({     type: "POST",     url: $(this).attr('action'),     data: $(this).serialize(),     success: function(response) {       $('#exportForm').unbind('submit');       console.log(response);     }   });   return false; }); 
like image 535
James Privett Avatar asked Nov 25 '13 14:11

James Privett


People also ask

How to prevent form submit twice JQuery?

Use JQuery to Prevent Multiple Form Submissions To prevent the user from submitting a form multiple times, we'll use JQuery to listen for the submission event. Once the form is submitted, we'll add a disabled attribute to the button to prevent multiple form submissions. Now the user can't click it again.

How stop Ajax call twice?

The Javascript/jQuery code If it's a button you could simply disable it at the start of the request and then enable it again in the callback function when the AJAX request has finished loading.


1 Answers

As well as calling preventDefault, also call stopImmediatePropagation on the event.

$('#exportForm').submit(function(e){     e.preventDefault();     e.stopImmediatePropagation();     $.ajax({         type: "POST",         url: $(this).attr( 'action' ),         data: $(this).serialize(),         success: function( response ) {             console.log( response );         }     });      return false; }); 
like image 95
Chris Sercombe Avatar answered Sep 20 '22 19:09

Chris Sercombe