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

10,722

Solution 1

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).

Solution 2

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

Share:
10,722

Related videos on Youtube

Gerwald
Author by

Gerwald

I am a Microsoft .Net Developer located in Europe/Austria. My Focus is ASP.NET MVC; HTML; JavaScript.

Updated on June 05, 2022

Comments

  • Gerwald
    Gerwald almost 2 years

    I am sure everybody knows that behaviour. You fill in a form on the web, and you submit it. After the submission you recognize that you filled in some wrong data. So, you click on the browsers back button. Then, sometimes the form still have the data you entered (what you are hoping in this situation) and sometimes not.

    I couldn't find any connection when it is cleared and when not.

    Some answers i found on the internet / stackoverflow:

    • on https connections, forms are always cleared
    • when using dynamic websites with sessions, forms are always cleared

    But both of them are definatly wrong. I have seen sites (like one of my own) that does keep the form-data after the browser back and are using https and are using sessions.

    So please: can anybody explain me how browsers are handling this stuff?

    By the way: my task is it to make sure that the form data is not cleared.

  • Gerwald
    Gerwald almost 12 years
    okay, when it is a browser issue: what browser acts how? eg. mobile Safari? And if so, how to get the browser to behave like i want? (keeping the data)
  • Gerwald
    Gerwald almost 12 years
    Thanks for your answer. Of course, you are completely right with your way of doing it. But this is quite a lot of work sometimes (especially if you need to add this behaviour afterwards). Life clould be much easier if only the browser could do the work. This is where my question is targeting to. Is there a way to get the browser to do the work!?