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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    To Prevent double submission

    Hi,

    How do I prevent user to reload/refresh or click on others option in the page after page submission? As this will cause it to reprocess the data which have already submitted.

    pls advise.....

  • #2
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    You can do this by 1 of 2 ways:

    The more preferred (and easiest) is to use Javascript. Check out the web for some "prevent double submission" scripts.

    The other method is to track something useful from your client computers (IP, hostname, browser, etc) using the Request.ServerVariables methods and track them in a database. Once you have that, then do a simple search and compare against the submission. If any records match, kill the submission.

    If you want to disable the refresh button or the back button, then you need to consider changing your code.

    HTH!
    To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
    Please, if you found my post helpful, pay it forward. Go and help someone else today.

  • #3
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,072
    Thanks
    4
    Thanked 8 Times in 8 Posts
    To prevent duplicate data when users either double clicking and/or click the back button and make a change in the data that they submitted you can also change your Database code to look for the existance of the data and if it exists then you Do an update instead of an insert. In SQL Server I use stored procedures to handle all input and will write my SPROCS accordingly. If you use inline SQL you can just add something like this


    Code:
    sSQL = "SELECT FieldA, FieldB, FieldC FROM MyTable WHERE FieldA = '" & sFieldA & "' AND FieldB = '" & sFieldB & "' AND FieldC = " & iFieldC
    oRs.Open sSQL, myConn
    'If Using ADO  
    If oRs.EOF Then oRs.AddNew()      
    oRs("FieldA") = sFieldA
    oRs("FieldB") = sFieldB
    oRs("FieldC") = iFieldC
    oRs.Update()
    oRs.Close
    'If Using SQL Inserts And Updates 
    If oRs.EOF Then
         oRs.Close
         sSQL = "INSERT INTO MyTable(FieldA, FieldB, FieldC) VALUES("' & sFieldA & "','" & sFieldB & "'," & iFieldC & ");"
        oRs.Open sSQL, myConn, 1,2
    Else
         oRs.Close
         sSQL = "UPDATE MyTable SET FieldA = '" & sFieldA & "', FieldB = '" & sFieldB & "', FieldC = " & iFieldC WHERE MyIDField = " iID
         oRs.Open sSQL, myConn, 1,2
    End If
    the above assumes that MyIDField is either an Access AutoNumber Field or a SQL Server Identity

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    I prefer to use javascript, so I have tried with below method:

    function checkReload()
    {
    if( document.formtest.checksub.value == "" )
    {
    document.formtest.checksub.value = "1";
    }
    else
    {

    alert("Your submission is in the process......Please wait, Resubmission is not allowed")
    }
    }


    </script>

    <body onLoad="checkReload();">

    <form name="formtest">
    testing
    <input type="hidden" name="checksub" value="" />
    </form>

    as I know that In most web browsers, hidden form variables are maintained across refreshes. That is, even though a page is refreshed, if a hidden form variable was changed by JavaScript before the refresh, the changed value will be used after the refresh. Thus, on a page load JavaScript can check to see if a hidden variable that defaults to being unset is set. If it is unset, it assumes this is not a refresh and it sets it. If it is set, it assumes this is a refresh.

    But I just wonder why the value of checksub won't change after checkReload, so the above method won't work for me.....kindly help.

    Well, I do found exactly the same method used, here to go http://www.tedpavlic.com/post_detect...javascript.php, I have tried their sample, cannot work as well.

    Pls advise....

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can this thread remove to "Javacript" category?

    Hi,
    Can this thread be removed to "Javacript" category?

    pls advise....

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by charon View Post
    as I know that In most web browsers, hidden form variables are maintained across refreshes. That is, even though a page is refreshed, if a hidden form variable was changed by JavaScript before the refresh, the changed value will be used after the refresh.
    That's not correct. And Javascript is not a reliable solution. The user can easily disable Javascript and then submit the page twice. The recommended solution is to keep track the time the user submits the form in a session variable and set the allowable time (say 10 sec) that same user (using his IP address) can submit again. If the same IP submits the page again in less than 10 secs, then stop processing the page. This is I think what CodingForums do when you do a search in its Search page.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #7
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    hidden form variables are maintained across refreshes is incorrect

    HI,
    as I know that In most web browsers, hidden form variables are maintained across refreshes. That is, even though a page is refreshed, if a hidden form variable was changed by JavaScript before the refresh, the changed value will be used after the refresh.
    You mean that above statement is incorrect?

    The user can easily disable Javascript and then submit the page twice
    Actually, I have been struggling about this when want to use the javascript in my application. Yup, I do agree this, so nowaday, what are the best solutions about this? Does it mean that javascript is not longer use as client side scripting?

    Pls advise......

  • #8
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by charon
    You mean that above statement is incorrect?
    Makes sense to me. Form values are maintained through refreshes, and I can't see that it makes a difference whether those values were changed by the user manually or by javascript automagically.

    Quote Originally Posted by charon
    Does it mean that javascript is not longer use as client side scripting?
    No, not at all. Otherwise what would all those AJAX-powered sites do? But you're talking about using JavaScript to validate a user action that could potentially break your back end system. This is not advisable. Sure, use JS for simple validation and stuff - but as an aid to the user, not as your actual "this is the bit that stops bad stuff happening to my database" code. If it's that critical to your application, check it server-side.


  •  

    Posting Permissions

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