Click here to Skip to main content
15,569,745 members
Articles / Programming Languages / Javascript
Technical Blog
Posted 30 Sep 2010

Tagged as


3 bookmarked

Preventing double submit on multi button forms with jquery

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
30 Sep 2010CPOL3 min read
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:

 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.

This article was originally posted at


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

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

GeneralForm submit to server Pin
Ajay Kale New18-Oct-10 21:43
Ajay Kale New18-Oct-10 21:43 
GeneralDisabling UI elements Pin
AWdrius30-Sep-10 22:51
AWdrius30-Sep-10 22:51 
Hi, actually disabling, say, text input boxes can be beneficial. Imagine that you have address form. For countries that do not have provinces/states (most countries except CA and US (-.) you do not want send bogus or unnecessary data back to server. So I'm hiding state/province input element (drop down box) *and* disabling it (prevents data from being sent to server). Together with server side validation IMHO it makes quite a nice routine.
Trust is a weakness.

GeneralRe: Disabling UI elements Pin
eglasius18-Oct-10 21:49
eglasius18-Oct-10 21:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.