How to prevent refresh page error while submitting POST form?

怎甘沉沦 提交于 2019-11-28 05:23:14

问题


When registration form submits using POST method and when you refresh the same page then it will prompt for resubmit or resend information. How we can prevent this?


回答1:


Use post-redirect-get. In short:




回答2:


You can use ajax. You keep the same php code that you put in another file, and you post the data via ajax.




回答3:


You can use include a one-time random token in the form. The first time that token is submitted (with other data), an action is taken. Future submissions of the same token have no effect (or just show the same confirmation page), and submissions with blank or invalid tokens are rejected.

This also protects against cross-site request forgery.




回答4:


Try this:

<?php
if (!empty($_POST)){
?>
    <script type="text/javascript">
        window.location = window.location.href;
    </script>
<?php } ?>

I placed it into prevent_resend.php and then included it after the postdata processing was done.

// ... save data from $_POST to DB
include('prevent_resend.php');
// ... do some other stuff


来源:https://stackoverflow.com/questions/5294378/how-to-prevent-refresh-page-error-while-submitting-post-form

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