22

I am having problem that when i am trying to submit the form by clicking on the submit button it takes some time to post request during this if i am again click on the Submit button it will again send the all parameters and parametrs get saved twice, thrice ....so on.

I don't know how to limit the the submit button so that form shouldn't get submitted twice. I think when i cliked on submit i have to disable submit button so that user can't click it again, is it right approach to doing this?

Salil
  • 46,566
  • 21
  • 122
  • 156

10 Answers10

40

Disabling the button is one solution, but potentially poses a problem for keyboard users who just hit enter to submit a form. In this scenario, the button won't be disabled. The sure-fire method would be to handle the onsubmit event like so:

(function () {
    var allowSubmit = true;
    frm.onsubmit = function () {
       if (allowSubmit)
           allowSubmit = false;
       else 
           return false;
    }
})();

(well, as sure-fire as you can get with JS enabled anyway). You could disabled the button as a visual confirmation to the end user that the form can only be submit once too.

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • 6
    True enough, I guess if someone can spastically hit the mouse, they can spastically hit the enter button too – Zoidberg Mar 30 '10 at 14:06
  • 1
    Very correct. It's also nice to show a message that the information is being sent (e.g., next to the submit button after it's been disabled). – Marcel Korpel Mar 30 '10 at 14:34
33
<input type="submit" onclick="this.disabled = true" value="Save"/>
Zoidberg
  • 10,137
  • 2
  • 31
  • 53
  • @JeffBleck Just tested it in Chrome, works for me, perhaps you are doing something wrong. – Zoidberg Nov 17 '11 at 12:47
  • Hi it seems not working on Chrome 17 as well. Mac os 10.6.8 , chrome 17.0 – icn Mar 25 '12 at 16:56
  • Works on Chrome 17 for me in Windows, sorry, I don't have a mac to test it – Zoidberg Mar 26 '12 at 10:44
  • 2
    Be aware that in case your button has a name attribute, disabling it prevents the value to be included in the POST/GET. – stracktracer Nov 11 '12 at 02:20
  • 3
    This doesn't prevent sending the form multiple times, see _Andy E_'s answer instead. – Tamás Bolvári Nov 21 '14 at 10:57
  • 1
    This is an unreliable solution (i know it's dated). Please use @Andy E solution instead. Reasons: 1) Keyboard enter, 2) for some Chrome, this will not submit the form at all, 3) button remains disabled on page refresh, 4) intrusive javascript – janechii Jun 24 '15 at 18:26
  • @janecii just add autocomplete="off" to fix that problem – Zoidberg Jun 29 '15 at 19:06
  • @Zoidberg fix to problem 3, maybe, that's a good one if it works cross browsers... but problems 1, 2 and 4 still remains. Why not use a more reliable solution? – janechii Jun 29 '15 at 22:39
  • @janechii My answer is quite dated, it also shows a simple example that does not involve a framework or extended amount of javascript. If we were to get into the 100% proper solution, imo, I would do away with the submit button and use a normal button. I would then collect the data from the form in Javascript and do an ajax post. This avoids a lot of complications present in the classic form system. But that would be a whole lot more code nobody would read. I think my answer does a good job at getting people started down the road to a proper solution, and I think that provides value. – Zoidberg Jul 01 '15 at 01:56
  • rather `onclick="this.style.visibility = 'hidden'"` – Chad Aug 09 '16 at 16:05
  • It's not working on chrome anymore. – Pooya Sabramooz Mar 17 '22 at 16:14
7

in chrome use this:

<input type="submit" onclick="this.form.submit();this.disabled = true;" value="Save"/>
user3024034
  • 117
  • 2
  • 4
4

I love the simplicity and directness of Zoidberg's answer.

But if you want to hook up that handler using code rather than markup (some people prefer to keep markup and code completely separated), here it is using Prototype:

document.observe('dom:loaded', pageLoad);
function pageLoad() {
    var btn;

    btn = $('idOfButton'); // Or using other ways to get the button reference
    btn.observe('click', disableOnClick);
}
function disableOnClick() {
    this.disabled = true;
}

or you could use anonymous functions (I'm not a fan of them):

document.observe('dom:loaded', function() {
    var btn;

    btn = $('idOfButton'); // Or using other ways to get the button reference
    btn.observe('click', function() {
        this.disabled = true;
    });
});

Doing it using jQuery will look very similar.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

I know this is old, but rather than disable, just hide the button, you can't click what you can't see.

<input type="submit" onclick="this.style.visibility = 'hidden'" value="Save"/>
Chad
  • 1,139
  • 17
  • 41
  • The problem with this is that it makes it invisible no matter what, even if there is form validation the button disappears :p – Jono Nov 17 '17 at 15:59
0

if you are use form submit / post in PHP then use this code

onclick="disableMe();" in type="submit" 

its submit button code

<input name="bt2" type="submit" onclick="disableMe();" id="bt2" style="width:50px;height:30px; margin-left:30px;" value="Select" />
VDWWD
  • 35,079
  • 22
  • 62
  • 79
user3314233
  • 43
  • 1
  • 1
0

Best way I found this to work best is if all information is correct on the form then add the attribute to the submit button. Incase user needs to comeback and fill out form again with correct information.

document.querySelector('.btn').addEventListener('submit',function(){
    if(input.value){
        this.setAttribute("disabled", "true")
    }
};
Brad Vanderbush
  • 173
  • 1
  • 13
0

You can add the next script to your 'layout page' (to be rendered on all the pages globally).

<script type="text/javascript">
    $(document).ready(function () {
        $(':submit').removeAttr('disabled'); //re-enable on document ready
    });
    $('form').submit(function () {
        $(':submit').attr('disabled', 'disabled'); //disable on any form submit
    });
</script>

Then, each time a form is submited, the submit buttons are disabled. (The submit button(s) are re-enabled when loading the page.)

If you are using a validation framework (like jQuery Validation), it is probably that you need to re-enable the submit button(s) when a validation fails also. (because the form validation failure makes that the submit be canceled).

If you are using jQuery Validation, you can detect the form invalidation (form submition cancellation) adding this:

$('form').bind('invalid-form.validate', function () {
    $(':submit').removeAttr('disabled'); //re-enable on form invalidation
});

Then if a form validation fails, the submit button(s) is/are enabled again.

tecla
  • 745
  • 7
  • 9
0

One solution is to not use an <input> as the submit button, but instead a <div> (or <span>) styled as a button, having an onclick attribute:

<form method="post" id="my-form">
  <input type="text" name="input1">
  <input type="text" name="input2">
  <input type="text" name="input3">

  ...

  <div class="button" id="submit-button" onclick="submitOnce()">
    Submit
  </div>
</form>

And the submitOnce function would first remove the onclick attribute and then submit the form:

function submitOnce() {
  document.getElementById('submit-button').removeAttribute('onclick');
  document.getElementById('my-form').submit();
}
Faheel
  • 2,435
  • 24
  • 33
0

you can use this .once() method to call once that function:

<form method="post" id="my-form">
  <input type="text" name="input1">
  <input type="text" name="input2">
  <input type="text" name="input3">

  ...

  <div class="button" id="submit-button" onclick.once="submitOnce()">
    Submit
  </div>
</form>
JOYAL SHAH
  • 63
  • 2
  • 6