Why are HTML forms sometimes cleared when clicking on the browser back button

China☆狼群 提交于 2019-11-30 05:02:36
Tim Medora

I can't provide the definitive answer for all scenarios.

As a web developer, here's the rule I generally follow for sites I develop, to accomplish the goal of not letting the user lose data:

  • Disable all caching (via HTTP headers) on the page you want retained.
  • Capture all data into session (or some other temporary storage) when the form is submitted.
  • If the user navigates backwards, the browser requests a new version of the page (because you set it to never cache).
  • The page has logic to look to the session/database/wherever and repopulates all the fields based on the last input state. If there were dynamic inputs on the page, you should have enough data to recreate them.
  • Once the process is finished, use a POST/Redirect/GET pattern to clear the session data and make it difficult for the user to go back to the original page.

Some answers i found on the internet / stackoverflow:
1. on https connections, forms are always cleared
2. when using dynamic websites with sessions, forms are always cleared

I believe #1 varies by browser/security settings/scenario.

Assumption #2 is certainly not true in all cases (the pattern I just described leverages session and dynamic forms).

jkoech

Its a browser issue. Browsers behave differently when the back button is clicked. This question was asked before here Losing form data when clicking the back button on browser

I find it funny that the link you provided was asked before this one but that one says its answered by this one. Oh and no accepted answers either.....

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