php 单击浏览器上的后退按钮时丢失表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1485672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:47:03  来源:igfitidea点击:

Losing form data when clicking the back button on browser

phpforms

提问by Oliver Bayes-Shelton

When I click back on any browser I lose all the data which was inputted in the form on both drops downs and text input types.

当我点击任何浏览器时,我会丢失在下拉菜单和文本输入类型的表单中输入的所有数据。

Is this a server , Browser or a coding issue ?

这是服务器、浏览器还是编码问题?

Thanks

谢谢

采纳答案by Salman A

Its a browser issue. Browser behave differently when the back button is clicked -- this behavior mostly depends on user privacy settings. In addition, most browsers will automatically reset form upon hitting back button for pages viewed over HTTPS connection regardless of their normal behavior for HTTP connections. In addition, many HTML forms that rely heavily on AJAX do not properly -- or consistently -- restore their previous state.

它的浏览器问题。单击后退按钮时,浏览器的行为会有所不同——这种行为主要取决于用户隐私设置。此外,大多数浏览器会在为通过 HTTPS 连接查看的页面点击后退按钮时自动重置表单,而不管它们对 HTTP 连接的正常行为如何。此外,许多严重依赖 AJAX 的 HTML 表单不能正确地——或一致地——恢复它们以前的状态。

Edit ----

编辑 - -

Now that you mention using PHP and MySQL, assuming that you are also using sessions... the browser will not pre-populate the form when you hit the back button. PHP-session powered pages are not cached in browsers and hitting back button to go back to previous page is just like opening that page again from the very beginning -- most browsers will not bother restoring the form values.

既然您提到使用 PHP 和 MySQL,假设您还使用会话……当您点击后退按钮时,浏览器将不会预先填充表单。PHP 会话驱动的页面不会缓存在浏览器中,点击返回按钮返回上一页就像从一开始再次打开该页面一样——大多数浏览器不会费心恢复表单值。

回答by Chung Lau

This applies to PHP and IE8.

这适用于 PHP 和 IE8。

Not only must you set cacheing to private, but you must remove the 4 cacheing headers and this can only be done with PHP 5.3.

您不仅必须将缓存设置为私有,还必须删除 4 个缓存标头,而这只能在 PHP 5.3 中完成。

In PHP 5.2 you can only set the 4 headers to blank values if using the Zend Framework's setHeader()method. For some reason is not sufficient on IE8 to set the 4 header values to empty values. Here's the code for PHP 5.3:

在 PHP 5.2 中,如果使用 Zend Framework 的setHeader()方法,您只能将 4 个标头设置为空值。出于某种原因,在 IE8 上不足以将 4 个标头值设置为空值。这是 PHP 5.3 的代码:

header_remove("Expires"); 
header_remove("Cache-Control"); 
header_remove("Pragma"); 
header_remove("Last-Modified"); 

回答by Christian

You need to create sticky formsfor the browser to keep form data when back is pressed. Its fairly easy to do, check out the tutorials in the search linked.

您需要为浏览器创建粘性表单以在按下后退时保留表单数据。它相当容易做到,请查看搜索链接中的教程。