简体   繁体   English

页面刷新后如何防止重复提交

[英]How to prevent double submission after page refresh

I have a 'contact us' html form and I'm giving gift for each member that click submit.我有一个“联系我们”的 html 表单,我会为每个点击提交的成员赠送礼物。 I want to make sure that a user doesn't submit twice (with different name and email).我想确保用户不会提交两次(使用不同的姓名和电子邮件)。 I can disable the button after click, but what will be the best solution to prevent submit after page refresh?单击后我可以禁用该按钮,但是防止页面刷新后提交的最佳解决方案是什么? Thanks谢谢

Is to directly redirect from your POST to a GET request or page.是直接从您的POST重定向到GET请求或页面。 if he/she refreshed it will refresh the other page.如果他/她刷新,它将刷新其他页面。 this is named Redirect After Post and you can read more about it here and in WikiPedia这被命名为 Redirect After Post,您可以 在此处WikiPedia 中阅读有关它的更多信息

An Example can be found here .可以在此处找到示例。

Two things that you can do and you should do.有两件事你可以做,你应该做。

  1. First disable the Submit button as soon as your client submits the form with valid information in it.一旦您的客户提交包含有效信息的表单,首先禁用Submit按钮。 So that he/she accidently doesn't send duplicate information in the form.以免他/她不小心在表单中发送重复信息。
  2. Second, which is more important.其次,哪个更重要。 Is before you save this information in your database whatever that is MySQL, MongoDB or etc. You perform a query that checks whether the information provided by the user exists in your db already or not.在您将此信息保存在您的数据库中之前,无论是 MySQL、MongoDB 还是其他。您执行查询以检查用户提供的信息是否已经存在于您的数据库中。 If not then proceed as you do but if it does you can do error handling on the use case scenario as per your business logic.如果没有,则继续执行,但如果确实如此,您可以根据您的业务逻辑对用例场景进行错误处理。
<script>
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>

See https://stackoverflow.com/a/45656609/1066234https://stackoverflow.com/a/45656609/1066234

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM