在浏览器中重新加载/刷新页面而无需重新提交表单? [英] Reload/refresh page in browser without re-submitting form?

查看:950
本文介绍了在浏览器中重新加载/刷新页面而无需重新提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交到同一页面的php页面上有一个表单。
我注意到,如果我重新加载/刷新页面,表单会重新提交。
如何以最简单的方式进行编码以避免这种情况?

解决方案

一种可能性是,重定向的方法。

简单地说,一个POST请求将永远不会传递给浏览器。相反,您执行所有必要的操作并在会话中存储所需的信息,然后使用代码303进行重定向。

  $ page ='show_result.php'; 
header('Location:'。$ page,true,303);
出口;

通过这种方式,浏览器将显示show_result.php页面(GET请求)而不是使用POST请求的页面。这也是添加到历史记录中的页面,因此刷新并使用后退按钮将永远不会执行另一个POST请求。作为一个很好的副作用,你摆脱了重发数据的浏览器警告,通常用户无法决定怎么做。



我认为这种方法最大的问题是,你需要一个会话来存储错误消息,这意味着你必须依靠cookie。如果您没有重定向来显示错误,浏览器将显示关于重新发送数据的警告。

I have a form on a php page that is submitted to the same page. I noticed that if I reload/refresh the page the form gets re-submitted. How do I code to avoid this in the most easy way?

解决方案

One possibility is, to implement the post-redirect-get approach.

Simply said, a POST request will be never delivered to the browser. Instead you execute all necessary actions and store the information you need in the session, and then you make a redirect with code 303.

$page = 'show_result.php';
header('Location: '.$page, true, 303);
exit;

Doing it this way, the browser will show the "show_result.php" page (a GET request) instead of the page requested with POST. This is also the page that is added to the history, so refreshing and using the back button will never do another POST request. As a nice side effect you get rid of browser warnings about resending data, normally the user cannot decide what to do then anyway.

I think the biggest problem with this approach is, that you need a session to store error messages, that means you have to rely on cookies. If you do no redirect to display errors, the browser will show the warning about resending data.

这篇关于在浏览器中重新加载/刷新页面而无需重新提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆