How do I stop the Back and Refresh buttons from resubmitting my form?

后端 未结 13 1172
时光说笑
时光说笑 2020-11-27 15:16

I am doing web development.

I have a page to do with credit card, which when user click \"refresh\" or \"Back\", the transaction will be performed one more time, wh

相关标签:
13条回答
  • 2020-11-27 15:41

    The standard way is to do it in 3 steps.

    1. the form page submits fields to processing page
    2. processing page processes data and redirects to result page
    3. result page just displays results, reloading it won't do any harm.
    0 讨论(0)
  • 2020-11-27 15:42

    I didn't see this here so here it is.

    1. Put a unique token in the form.
    2. The submit button triggers an xmlhttp(ajax) request to the server to create a session variable named after the token with a stored value of 1.
    3. The ajax request submits the form after receiving a positive state change.
    4. The form processing script checks for the session variable withe the stored value of 1.
    5. The script removes the session variable and processes the form.

    If the session variable is not found, the form will not be processed. Since the variable is removed as soon as its found, the form can only be run by pressing the submit button. Refresh and back will not submit the form. This will work without the use of a redirect.

    0 讨论(0)
  • 2020-11-27 15:43

    Place this code on the form page

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    
    Response.Cache.SetExpires(DateTime.Now-new TimeSpan(1,0,0));
    
    Response.Cache.SetLastModified(DateTime.Now);
    
    Response.Cache.SetAllowResponseInBrowserHistory(false);
    
    0 讨论(0)
  • 2020-11-27 15:43

    The best way is to have enough session handling logic that you can recognise the 2nd (and onwards) attempt as "this is just a re-submission" and ignore it.

    0 讨论(0)
  • 2020-11-27 15:45
    1. generate a random string and store it in session,

    2. then output it to your form as a hidden value,

    3. check the submitted and store variable, if matches process your request,

    4. go to 1.

    0 讨论(0)
  • 2020-11-27 15:45

    vartec:s solution solves the reload-problem, not the back-problem, so here are a solution to that:

    1. The form page sets a session variable, for example session("fromformpage")=1
    2. The processing page check the session variable, if its ="1" then process data and redirect to result page if any other than ="1" then just redirect to result page.
    3. The result page sets the session variable to "".

    Then if the user is pressing back button, the processing page will not do the process again, only redirect to process page.

    0 讨论(0)
提交回复
热议问题