Disable Submit Button Upon Form Submission

By  on  

Multiple clicks on a "Submit" button can cause duplicate processing if your programming is slow or the user's connection is lagging. Duplicate processing can include duplicate database records, multiple emails, or different types of errors. JavaScript provides an easy way to disable the multiple submission of a form once the form has been submitted.

The Code

In the HTML, use the following "onsubmit" information:

It's important to trigger the even on form submission -- not everyone clicks the "submit" button. You will also need to give your submit button the submit_button ID attribute.

Recent Features

  • By
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Animated AJAX Record Deletion Using MooTools

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with MooTools JavaScript. The PHP - Content & Header The following snippet goes at the...

  • By
    Create a Simple News Scroller Using MooTools, Part I:  The Basics

    News scroller have been around forever on the internet. Why? Because they're usually classy and effective. Over the next few weeks, we'll be taking a simple scroller and making it into a flexible, portable class. We have to crawl before we...

Discussion

  1. Dave Doyle

    As an aside, if you’re also using any javascript validation, disable the submit button only after you’ve passed the validation tests.

    Also, if the form is output programattically and the results stored in a DB, you can incorporated a unique hidden value in the form and redirect to an error page on duplicate submission detection.

  2. Great tips (and name) Dave!

    I’ve never been a fan of “disabling” elements on a website, but if it can save a developer lots of cleanup in the long run, I say do it.

  3. Hugo

    How would you write a code to “redirect to an error page on duplicate submission detection.”

  4. Hugo

    oh, and my middle name is David. It may not be Dave but it is pretty close. :D

  5. thanks for the tip… might also be a good idea to apply some sort of visual display so your users know the server is working. :)

  6. Rohini

    Hi David,

    I’m having a really hard time trying to disable a submit (IMAGE) button when the page is submitted in APEX 3.2.
    I cannot get the SUBMIT button to disable itself. Somehow this only works on HTML Buttons rather than IMAGES from CSS Templates.
    Would appreciate it if you could suggest an alternative.
    Thanks.

  7. Great way of prevention. Thanks a lot. I just implemented it on one of my websites.

  8. Ava

    Does anyone have a unit test code for testing the submit button disabled when click it?

  9. roshan

    Thank boss..u saved my time

  10. manoj

    i can’t disable the submit button because i have some validation before page submit.if the validation failed the button is permanently disabled please some one assist

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!