Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

Preventing double submit on multi button forms with jquery

4.33/5 (3 votes)
30 Sep 2010CPOL3 min read 16.1K  
Preventing double submit on multi button forms with jquery

Note that this is just a fast feedback mechanism, it shouldn't replace server side logic to prevent double submits. This is specially true if the operations can't be easily reverted or there would be associated costs to reversing the operation.

With that out of the way, let's get right to the code:

JavaScript
 1: $(document).ready(function () {
 2:     $('form').submit(function (e) {
 3:         var theForm = $(this);
 4:         var isValid = doSomeFormValidations(theForm);
 5:         if (!isValid) return false;
 6:         setTimeout(function () {
 7:             var btn = theForm.find('input[type="submit"]');
 8:             btn.attr('disabled', 'disabled');
 9:             btn.val('Processing');
10:          }, 10);
11:         return true;
12:     });
13: });

Replace doSomeFormValidations with your own logic and you are good to go. There are other alternatives you might already be using, like having the button disabled until all validations pass. The important bit there is to make sure it'll only disable the buttons if all validations passed.

Now let's go over some pieces and why those are relevant to it working with multi button forms.

  • Return true/false in the submit body: true moves forward with the submit, while false cancels it.
    • An alternative to this is returning false, unbinding our submit function and explicitly calling theForm.submit(). While this seems to work in the multiple button scenario, we are playing with fire here. We are relying on jquery or the underlying browser events/functions to send that last clicked button’s value. This is an implicit assumption we would be making, and it's usually based on works on my machine.
  • Disabling the submit buttons & adding a processing message for the user: 
    • No delay: has the undesired side effect that the button’s value isn't sent with the request in some browsers. Additionally it can even prevent the submit if it were disabled on the button’s click instead of the form’s submit. This is not totally unexpected as we are disabling the button. In fact, disabling any other input element (textboxes, select, etc.) can cause that data not to be sent to the server just like with our selected button’s value.
    • Delay: By adding an insignificant delay, we let the rest of the submit behavior do its thing, so all the values in the form are sent in the request, including the selected button’s value. After those values are grabbed by the submit, we can disable controls and change values without affecting what will be sent to the server.
  • Don’t call use an id/name of ‘submit’ in your buttons. While not shown in the above code, this is important as it could cause conflicts with the submit events.

I want to stress out what I just mentioned in the no delay bullet. Just disabling any input of the form right away will prevent the value from being sent to the server in some browsers.

This can easily get you into a situation where you can't figure out why the button pressed isn't being sent to the server, specially if you are working on an already built third party site. So if you are missing any input, first use a developer tool to inspect the elements and then go through the scripts, it's very likely somewhere in there.

You can tweak the above function to fit your needs, like display a different message, hide part of the UI, take actions if it'll timeout.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)