How to prevent form resubmission when page is refreshed (F5 / CTRL+R)

后端 未结 23 1290
醉话见心
醉话见心 2020-11-22 00:46

I have a simple form that submits text to my SQL table. The problem is that after the user submits the text, they can refresh the page and the data gets submitted again with

23条回答
  •  醉话见心
    2020-11-22 01:39

    I found next workaround. You may escape the redirection after processing POST request by manipulating history object.

    So you have the HTML form:

    When you process this form on your server you instead of redirecting user to /the/result/page by setting up the Location header like this:

    $cat process.php
    
    

    After processing POSTed data you render small

    The result is:

    as you can see the form data is POSTed to process.php script.
    This script process POSTed data and rendering /the/result/page at once with:

    1. no redirection
    2. no rePOST data when you refresh page (F5)
    3. no rePOST when you navigate to previous/next page through the browser history

    UPD

    As another solution I ask feature request the Mozilla FireFox team to allow users to setup NextPage header which will work like Location header and make post/redirect/get pattern obsolete.

    In short. When server process form POST data successfully it:

    1. Setup NextPage header instead of Location
    2. Render the result of processing POST form data as it would render for GET request in post/redirect/get pattern

    The browser in turn when see the NextPage header:

    1. Adjust window.location with NextPage value
    2. When user refresh the page the browser will negotiate GET request to NextPage instead of rePOST form data

    I think this would be excelent if implemented, would not? =)

提交回复
热议问题