Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Aug 2007
    Posts
    41
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Disable Form Button, Back Browser Button...

    Hey there,

    My client was having an issue on his website where forms were being submitted more than once if the user clicked a submit button really fast.

    The solution I found is as follows:

    Code:
    function disableSubmit(whichButton) 
    { 
        if (document.getElementById) 
        { 
            // this is the way the standards work 
            document.getElementById(whichButton).disabled = true; 
        } 
        else if (document.all) 
        { 
            // this is the way old msie versions work 
            document.all[whichButton].disabled = true; 
        } 
        else if (document.layers) 
        { 
            // this is the way nn4 works 
            document.layers[whichButton].disabled = true; 
        } 
    }
    Here is an example of a simple form that uses this function:

    Code:
    <form name="myLoginForm" id="myLoginForm" method="post" 
    action="../member-login.cfm?login=1" onsubmit="disableSubmit('myLoginBtn');">
            
    Username: <input id="MY_USERNAME" name="MY_USERNAME" type="text" 
    class="text-very-small" maxlength="50"/>&nbsp;&nbsp; 
    
    Password: <input id="MY_PASSWORD" name="MY_PASSWORD" type="password" 
    class="text-very-small" maxlength="50"/>&nbsp;&nbsp;
    
    <input type="submit" value="Login" title="Login" id="myLoginBtn" 
    name="myLoginBtn" class="myButtonStyle" />
            
    </form>
    This fixes that issue for users that have javascript enabled, but it creates another issue. When the user clicks the back button on their browser, the submit button is disabled.

    You can view this behavior by clicking the search button on the top menu of this page:

    http://www.greenhappenshere.com/find...businesses.cfm

    After you click search, click the back button on your browser and notice how the search button is already disabled.

    So I need a solution to this problem or a better way of disabling form buttons so forms do not get submitted more than once.

    Thanks in advance for any assistance you can provide me with.

    Sincerely,
    Travis Walters

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by twalters84 View Post
    This fixes that issue for users that have javascript enabled, but it creates another issue. When the user clicks the back button on their browser, the submit button is disabled.
    Why the user would click the back button? Why the user would click quickly twice the submit button?

    Following the line of "what if" the developer will never finish the work.

    What if tomorrow will be launched a browser which will submit twice a form by default, even the user will click only once? What if the user wants to click the right mouse, not the left mouse?

    Tell your client that it is a small chance that a user would click twice on a submit button, I agree. Tell him that it might be a small chance that the user would click the back button, I admit that too. But tell your client that it is very improbable that the user would do both those stupid actions same time or subsequently.

    Usually, those problems of accidentally double login trials must be solved at the server-side level (using the session controls in php, asp, or whichever server language you use), not with javascript at the client-side level.
    Last edited by Kor; 10-09-2008 at 08:36 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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