Prevent form from being submitted twice

被刻印的时光 ゝ 提交于 2019-11-27 03:26:21

问题


I have a simple php form, like this:

<?php

if(isset($_POST['myform']))
   // email...
else
   // display form

the problem is that if I refresh the page after I submit the form, it gets submitted twice. How can I prevent this from happening?


回答1:


You should perform a REDIRECT to a page with a message that the data was inserted... and a back button to go to the form again (if you want to)...

To redirect using PHP use the header function:

header('Location: http://www.example.com/');



回答2:


Perform a redirect after the data is inserted to a confirmation page (can be done with header(), which should clear out the POST data and allow for refreshing without duplicating content.




回答3:


In the case of user having lag and he hits the submit button a few times, then maybe use client side mechanism, use js to set the submit button to disabled once it is clicked. but will have to display message saying roughly something like, "sending message ... if no response pls reload page and retry".




回答4:


session_start();    
if (!$_SESSION['REQUEST_TYPE_USER_ID'] == $_POST)
{
//your code
//after the success process
    $_SESSION['REQUEST_TYPE_USER_ID'] = $_POST;
}
else
{
// request duplicated
}


来源:https://stackoverflow.com/questions/3614197/prevent-form-from-being-submitted-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!