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

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

    How to prevent php form resubmission without redirect. If you are using $_SESSION (after session_start) and a $_POST form, you can do something like this:

    if ( !empty($_SESSION['act']) && !empty($_POST['act']) && $_POST['act'] == $_SESSION['act'] ) {
      // do your stuff, save data into database, etc
    }
    

    In your html form put this:

    
    
    

    So, every time when the form is submitted, a new act is generated, stored in session and compared with the post act.

    Ps: if you are using an Get form, you can easily change all POST with GET and it works too.

提交回复
热议问题