如何避免表单重新提交

如何避免表单重新提交

本文介绍了如果选择浏览器后退按钮,如何避免表单重新提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  1. Symfony2为实体提供了删除表单

  2. 用户选择删除按钮

  3. Symfony2控制器将删除实体并将用户重定向至确认页面

  4. 如果用户选择浏览器后退按钮,他/她再次查看记录和删除表单

  5. 选择删除按钮会抛出一个异常,因为它已被删除

避免后退按钮和重新提交的最佳方法是什么?

此问题的控制器与

所有数据更改请使用 POST 方法 - 或 PUT POST DELETE ,如果您使用RESTful方式执行此操作。在这样的请求之后,重定向到 GET 页面。如果用户按下后退按钮,浏览器将显示一条警告,说明请求应该再次发送,并且可能导致不需要的结果。如果用户坚持重新发送请求,她应该得到一个 404 错误页面,因为她正在尝试使用一些不存在的东西。


Let's consider a scenario like below :

  1. Symfony2 provides a removal form for an entity
  2. User selects remove button
  3. Symfony2 controller will remove the entity and redirect the User to confirmation page
  4. If user select browser back button he/she will see the record and removal form again
  5. Selecting Remove button will throw an exception because it was removed already

What is the best way to avoid back button and re-submissions ?

The controller for this question is the same as here.

解决方案

All data changing requests should be done using the POST method — or PUT, POST or DELETE if you're doing it in the RESTful way. After such a request, redirect to a GET page. If a user will push the back button, the browser will show a warning saying that the request should be sent again and that it can lead to unwanted results. If the user insists on resending the request, she should get a 404 error page because she's trying to do stuff with something that doesn't exist anymore.

这篇关于如果选择浏览器后退按钮,如何避免表单重新提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:05