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

后端 未结 23 1656
醉话见心
醉话见心 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:29

    I searched for solution to prevent resubmission in a huge project afterwards. The code highly works with $_GET and $_POST and I can't change the form elements behaviour without the risk of unforeseen bugs. So, here is my code:

    
    
        
            GET/POST Resubmit
        
        
    
        

    Forms:

    GET Form:

    POST Form:

    POST Form with GET action:

    File Upload Form:

    Results:

    GET Form Result:

    text_get:

    POST Form Result:

    text_post:

    POST Form with GET Result:

    text_get2:

    text_post2:

    File Upload:

    file:

    It only works to avoid the resubmit of $_POST, not $_GET. But this is the behaviour I need. The resubmit issue doesn't work with file uploads!

提交回复
热议问题