Preventing form resubmission

前端 未结 12 934

Page one contains an HTML form. Page two - the code that handles the submitted data.

The form in page one gets submitted. The browser gets redirected to page two. P

相关标签:
12条回答
  • 2020-11-22 09:40

    Directly, you can't, and that's a good thing. The browser's alert is there for a reason. This thread should answer your question:

    Prevent Back button from showing POST confirmation alert

    Two key workarounds suggested were the PRG pattern, and an AJAX submit followed by a scripting relocation.

    Note that if your method allows for a GET and not a POST submission method, then that would both solve the problem and better fit with convention. Those solutions are provided on the assumption you want/need to POST data.

    0 讨论(0)
  • 2020-11-22 09:42

    If you refresh a page with POST data, the browser will confirm your resubmission. If you use GET data, the message will not be displayed. You could also have the second page, after saving the submission, redirect to a third page with no data.

    0 讨论(0)
  • 2020-11-22 09:46

    You need to use PRG - Post/Redirect/Get pattern and you have just implemented the P of PRG. You need to Redirect. (Now days you do not need redirection at all. See this)

    PRG is a web development design pattern that prevents some duplicate form submissions which means, Submit form (Post Request 1) -> Redirect -> Get (Request 2)

    Under the hood
    

    Redirect status code - HTTP 1.0 with HTTP 302 or HTTP 1.1 with HTTP 303

    An HTTP response with redirect status code will additionally provide a URL in the location header field. The user agent (e.g. a web browser) is invited by a response with this code to make a second, otherwise identical, request to the new URL specified in the location field.

    The redirect status code is to ensure that in this situation, the web user's browser can safely refresh the server response without causing the initial HTTP POST request to be resubmitted.

    Double Submit Problem
    

    Post/Redirect/Get Solution
    

    Source

    0 讨论(0)
  • 2020-11-22 09:47

    There are 2 approaches people used to take here:

    Method 1: Use AJAX + Redirect

    This way you post your form in the background using JQuery or something similar to Page2, while the user still sees page1 displayed. Upon successful posting, you redirect the browser to Page2.

    Method 2: Post + Redirect to self

    This is a common technique on forums. Form on Page1 posts the data to Page2, Page2 processes the data and does what needs to be done, and then it does a HTTP redirect on itself. This way the last "action" the browser remembers is a simple GET on page2, so the form is not being resubmitted upon F5.

    0 讨论(0)
  • 2020-11-22 09:52

    I really like @Angelin's answer. But if you're dealing with some legacy code where this is not practical, this technique might work for you.

    At the top of the file

    // Protect against resubmits
    if (empty($_POST))  {
       $_POST['last_pos_sub'] = time();
    } else {
         if (isset($_POST['last_pos_sub'])){
            if ($_POST['last_pos_sub'] == $_SESSION['curr_pos_sub']) {
               redirect back to the file so POST data is not preserved
            }
            $_SESSION['curr_pos_sub'] = $_POST['last_pos_sub'];
         }
    }
    

    Then at the end of the form, stick in last_pos_sub as follows:

    <input type="hidden" name="last_pos_sub" value=<?php echo $_POST['last_pos_sub']; ?>>
    
    0 讨论(0)
  • 2020-11-22 09:52

    use js to prevent add data:

    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
    
    0 讨论(0)
提交回复
热议问题