SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)

    Best practise for disabling submit button using JavaScript

    Hi,

    I have a simple PHP form and to prevent double data submission, once the user has clicked 'submit', I want to disable the submit button using JavaScript.

    It was recommended in another thread that one could accomplish this using jQuery and the following code:

    Code JavaScript:
    $('input[type="submit"]').click( function() { $(this).attr('disabled','disabled'); } );

    However, I also read that some browsers don't like having the submit button disabled, and that a more "elegant" way to do things would be:

    Code JavaScript:
    <form .. .. onsubmit="if (this.getAttribute('submitted')) return false; this.setAttribute('submitted','true');">

    Ignoring the inline js for a minute, does the second method have any (dis)advantages over the first.

    Does anybody have another method they use to achieve the same thing?

    Thanks in advance.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,892
    Mentioned
    138 Post(s)
    Tagged
    2 Thread(s)
    That was me who gave you the code and indeed I had it the wrong way around. You shouldn't disable the button onclick, but on form submit. If you disable the button onclick IE won't submit the form anymore (all other browsers will though, in case you're interested).

    This should work you have jQuery

    Code javascript:
    $("form").submit( function() { $(":input[type=\'submit\']").attr("disabled","disabled"); });

    My bad, sorry

    Your code should also work. I've never seen that approach; looks interesting
    Rmon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,233
    Mentioned
    47 Post(s)
    Tagged
    1 Thread(s)
    Does disabling the act of submission rather than disabling the actual submit button take care of those who hit Enter instead of hitting the submit?

    If people are double-submitting, though, I'd start looking around to see what's making your form take so long to be processed. We had a similar problem (people re-submitting) and it turned out the back-end processing was going up to a whole minute long, which is way too long to give response to a user that the form was successfully submitted.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,312
    Mentioned
    177 Post(s)
    Tagged
    8 Thread(s)
    Does disabling the act of submission rather than disabling the actual submit button take care of those who hit Enter instead of hitting the submit?
    Indeed it does. With this code it is also not possible to submit the form more than once using enter, which is nice.

    If people are double-submitting, though, I'd start looking around to see what's making your form take so long to be processed. We had a similar problem (people re-submitting) and it turned out the back-end processing was going up to a whole minute long, which is way too long to give response to a user that the form was successfully submitted.
    Good point, but this site is just intended for a few (elder) relatives / friends and all I want to do is avoid them (the relatives) submitting stuff over and over again (which they are amazingly good at).

    I had the same problem with a site for work which I wrote using Ruby on Rails. In that case I generated a hash of their entire submission and stored it in a session variable which I could then check before writing anything to the db.

    Unfortunately my PHP skills are not quite as good ...

    Thanks for all the help.

  5. #5
    om nom nom nom Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,233
    Mentioned
    47 Post(s)
    Tagged
    1 Thread(s)
    (which they are amazingly good at)
    It should never amaze us how easily users can break our stuff... but we still are : )


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •