Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    791
    Rep Power
    30

    Prevent from posting twice


    If you have a post page that does inserts into the database, what is the best way to prevent double posting where a user might use their back arrow key and refresh?

    I know you can do a redirect after the post and that usually works but perhaps there are ways I am not familar with.

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    49
    Rep Power
    3
    An article posted on 4guysfromrolla shows this technique:

    <script>
    function DisableButton(b)
    {
    b.disabled = true;
    b.value = 'Submitting';
    b.form.submit();
    }
    </script>

    <p>
    Click the button to see how it can be disabled and
    have its text changed when clicked...
    </p>

    <input type="submit" name="SubmitButton" id="SubmitButton"
    value="Submit"
    onclick="DisableButton(this);" />

    Even after a redirect, one could still go back and resubmit or refresh a non-redirected page to resubmit the form.

    If you don't mind using a session variable this will handle everything...

    <form method="post" action="result.asp">
    Enter data to save here:
    <input type="text" name="form1">
    <input type="submit" value="Submit">
    </form>

    result.asp
    <%
    lastdata = session("form")
    newdata = request.form("form1")
    if lastdata = newdata then
    response.write "You are submitting the same information twice."
    response.end
    else
    session("form") = newdata
    end if
    %>
  4. #3
  5. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    791
    Rep Power
    30
    I was thinking sessions would work but not sure the best way to code that. I will give it a try - looks good. Thanks for your response and suggestion.

    I appreciate your help.
  6. #4
  7. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    791
    Rep Power
    30
    The session code is not allowing me to submit even on the first try. I get the response.write message and it won't submit.
  8. #5
  9. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Columbus, OH
    Posts
    1,608
    Rep Power
    278
    just make result.asp simpler
    <%
    if session("submitted") <> "yes" then
    response.write "You are submitting the same information twice."
    response.end
    else
    session("submitted") = "yes"
    end if
    %>
    GK
    __________________________________________________ _____
    if you found this post is useful click Give Rep button (bottom side on this reply ) and agree
  10. #6
  11. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    49
    Rep Power
    3
    If you set session submitted to 'yes' though, it will remain as 'yes' for subsequent form submissions even with different values inputted into the form and be rejected (until the session times out or you close the browser).

    The code that I posted works... you have to save it as two files, one being the html form and the second as 'result.asp" with the ASP code given. It was tested and works fine, not sure why you're receiving an error unless you have the incorrect form name and it's pulling up null every time.
  12. #7
  13. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Columbus, OH
    Posts
    1,608
    Rep Power
    278
    sqlconfused

    Sure session("submitted") = "yes" just for one page in this case you need to create something session("Form22")= "yes" and for next page session("Form23")= "yes" and so on. It will eat your server memory, but it is simple solution. And usually it is just a few pages so, it should not be a problem...
  14. #8
  15. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    791
    Rep Power
    30
    <form method="post" action="result.asp">
    Enter data to save here:
    <input type="text" name="form1">
    <input type="submit" value="Submit">
    </form>

    result.asp
    <%
    lastdata = session("form")
    newdata = request.form("form1")
    if lastdata = newdata then
    response.write "You are submitting the same information twice."
    response.end
    else
    session("form") = newdata
    end if
    %>
    Perhaps this is my error. I put..

    <form method="post" action="result.asp" name="form1">

    Rather than

    <input type="text" name="form1">.

    Could that make a difference? And if so, can I make it a hidden field?

    Also, wouldn't you have issues with subsequent submissions with different customers if the browser wasn't closed and the session not terminated?

    I already that a Customerid in session. Could that be added to your code to prevent that issue.

    Thanks!
  16. #9
  17. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    49
    Rep Power
    3
    1) Put the "form1" in the text input not the form name. A form might have 10 different text input fields with different values whereas a form only has one name so you need to specify which field it is you want.

    2) You can make the input field hidden. If you're trying to prevent double posting of user input though I would imagine there would already be a plain text input field that the user is submitting.

    The code that I posted will prevent double posting but still allow NEW/DIFFERENT text to be posted.

    3) Also, wouldn't you have issues with subsequent submissions with different customers if the browser wasn't closed and the session not terminated?

    Only if the customer was submitting the EXACT same information. I can't see a situation where you'd have multiple customers using the same machine and where you'd not want the same information to be posted by two different customers.


    ---- somefile.asp ----

    <% Session.Contents.Remove("form") %>
    <form method="post" action="result.asp">
    Enter data to save here:
    <input type="text" name="form1">
    <input type="submit" value="Submit">
    </form>

    ---- result.asp ----
    <%
    lastdata = session("form")
    newdata = request.form("form1")
    if lastdata = newdata then
    response.write "You are submitting the same information twice."
    response.end
    else
    session("form") = newdata
    end if
    %>

    This will remove the session at the input information stage and prevent refreshing once it is posted. A customer could go back to the form and re-type the same data as last time and it would work. Again, I can't see a scenario where you would not want two different customers to be able to submit the same data.
  18. #10
  19. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    791
    Rep Power
    30
    First of all, thank you for your help.

    I have posted the exact data (copy and paste) in my form. I added the Session.Contents.Remove("form") also on the form page.

    Then at the top of my result.asp page I copy and paste the exact code.

    When I make my first attempt to submit the form I get the response.write message and the form doesn't execute because of the response.end.

    Not sure what else I can do.

    When I try to analyze your code, lastdata = newdata would occur the first time you hit the results page because you create that before the if statement. Maybe I am incorrect in that analysis.
  20. #11
  21. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    49
    Rep Power
    3
    I have just tested it and it works fine. New data is processed, the same data as the last submit (or a refresh) gives the error message and stops.

    There are a few things to consider... if you just press enter without submitting ANY data, you WILL get the error message.

    lastdata pulls the data from the session which is the LAST form submitted, newdata pulls data from the form which is the NEW information. So they are not going to be equal all the time UNLESS you submit the same data as last time (the form = session).

    One correction and possibly an answer as to why you're experiencing trouble. If you submit nothing (just press enter on the form) then the session WILL equal the form data because both will be null. If you're seeing the error message I surmise that you're still not getting the form data field correct.

    Perhaps we should expand on this for debugging..


    <%
    lastdata = session("form")
    newdata = request.form("form1")
    response.write "<br>The old data was:" & lastdata
    response.write "<br>The new data is:" & newdata

    if newdata = "" then
    response.write "No text was entered into the form"
    response.end
    end if

    if lastdata = newdata then
    response.write "<br>You are submitting the same information twice."
    response.end
    else
    session("form") = newdata
    response.write "<br>New data has been submitted and stored."
    end if
    %>

    Perhaps you might post your form code to be analyzed....

    (ps this forum server is very very slow)
  22. #12
  23. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    791
    Rep Power
    30
    Sorry for the delay in responding, I have been out of town.

    Perhaps you might post your form code to be analyzed....
    Code:
    session ("ProductID") = ProductID
    session ("CustomerID") = CustomerID
    
    
    <form method="post" action="results.asp">
    
    <input type="hidden" name="ProductID" value="<%=ProductID%>">
    <input type="hidden" name="CustomerID" value="<%=CustomerID%>">
    
    <input type=submit value="Submit" id=submit3 name=submit1>
    I can't see a scenario where you would not want two different customers to be able to submit the same data.
    I am deleting the ProductID in the form post and replacing it with a different Product as a replacement in the post page. So if, let's say a husband and wife on same computer wanted the same replacement, after one customer gets the replacement and then the other one uses a back arrow key to log into their account, the newly created form session could still be in session to prevent the second replacement.

    Could there be a work around in this situation.
  24. #13
  25. No Profile Picture
    Contributing User
    ASP Explorer (0 - 99 posts)

    Join Date
    Oct 2012
    Posts
    49
    Rep Power
    3
    I think you are really overanalyzing things though. What are the odds that two people will use the same machine to place the same order back to back? Even if they do, a checkout cart will be in place and they'll see the final amount payable.

    Code:
    session ("ProductID") = ProductID
    session ("CustomerID") = CustomerID
    
    
    <form method="post" action="results.asp">
    
    <input type="hidden" name="ProductID" value="<%=ProductID%>">
    <input type="hidden" name="CustomerID" value="<%=CustomerID%>">
    
    <input type=submit value="Submit" id=submit3 name=submit1>
    
    results.asp
    
    <%
    lastdata = session("form")
    newdata = request.form("productID")
    if newdata = "" then
    response.write "No product ID was found error. Please go back and try again."
    response.end
    end if
    
    if lastdata = newdata then
    response.write "<br>You are ordering the same item twice error."
    response.end
    else
    session("form") = newdata
    response.write "<br>Your item has been added to the cart."
    end if
    %>
  26. #14
  27. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Mar 2005
    Location
    Columbus, OH
    Posts
    1,608
    Rep Power
    278
    Sorry I was out of town and did not respond early, but it looks like you working just on standard check out pages. All what you need is to after customer first added something to order track in database order id, activity time and order items order id needs to be stored on customer page, hidden field or session variables or query string. Every time customer submitted page you should check if order Id present - update activity time. It could be one file which you can include in all your pages as include statement. in addition to that inside that file you should include code to delete all info about all expired orders. So if somebody start shopping and leave the page and another person added item to order the first things you update or create order with current server time, delete all expired orders and items belong to them and adding items to current order. and when sending response to user stick to hidden field or session or query string new order id. I usually using SQL server for storing data and unique identifier field for order id which is 25 char long and secure enogh
  28. #15
  29. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    791
    Rep Power
    30
    Thanks for your suggestions, as always, I appreciate the time you took to make your comments.
Page 1 of 2 12 Last
  • Jump to page:

Similar Threads

  1. Posting a URL in asp
    By wildcats581 in forum ASP Development
    Replies: 1
    Last Post: October 6th, 2011, 09:31 PM
  2. Posting
    By Ch1n in forum HTML, JavaScript And CSS Help
    Replies: 1
    Last Post: September 20th, 2005, 09:48 AM
  3. POSTing to URL through code
    By Charles in forum .NET Development
    Replies: 1
    Last Post: March 15th, 2004, 04:45 PM
  4. posting a form
    By jsherman in forum Visual Basic Programming
    Replies: 4
    Last Post: January 30th, 2004, 01:20 PM

IMN logo majestic logo threadwatch logo seochat tools logo