0

I have a multipage form, with back buttons, and I'm trying to have a server-side way to maintain the form data if the user clicks back.

I have a back button like this:

<button id="backButton">Back</button>

And the button handler is as such:

$('#backButton').live('click',function() {
        window.history.back();
    });

After the first part of the form is submitted, I put all of the submitted data into $_SESSION variables. I can even echo out those variables on the second part of the form, but when I click the back button to go to the first part of the form, the session variables don't carry over.

I am starting the session correctly and I use session variables for various other parts of the site, and they work flawlessly.

How can I carry variables back when a back button is pressed?

Mu Mind
  • 10,935
  • 4
  • 38
  • 69
AKor
  • 8,550
  • 27
  • 82
  • 136
  • Check that the value in the session cookie stays the same between all the requests. If it changes, then you're most likely getting a new empty session for some reason - e.g.: the session cookie isn't "sticking" – Marc B May 05 '11 at 19:06
  • 1
    Are you sure your browser isn't caching a copy of the page? Try to ["hard refresh"](http://ascherconsulting.com/how/do/i/perform/a/hard/refresh/in/firefox/) it -- does that help? – jedwards May 05 '11 at 19:08
  • Yes, it is not caching a copy of the page. – AKor May 05 '11 at 19:48

2 Answers2

1

Sennheiser, I can't get a handle on your exact situation, but I can get your problem. Here are my thoughts:

  1. When the user hits 'back', they usually load the page from the browser cache. Ergo, the browser is not asking PHP to re-parse the page with the session variables. Try hitting shift or Control F5 to re-request the page from the server.
  2. #1 being successful or not is irrelevant, since no user is going to refresh the page thinking their results will return. You have to rely upon the browser OR JavaScript.
  3. A brief review, looking this up, makes me think the following solutions are your best bet:

You can either..

  • Use JavaScript to retrieve the form data and save the contents to a cookie, which will be loaded (Safely!! Look out for XSS..) whenever the form is loaded. You seem to use jQuery, that'll help! I'm unsure of the safety, but read up on $.('#formID').serialize().

  • Rely upon the browser's native form-saving components. While doing a review of stack overflow, I found a similar question that had an answer that can help you. In summation, if your forms are automatically generated by JS or the page is non-cacheable (likely https, or your server says no-cache), you cannot rely upon the browser to save the form.

Let me know if my answers helped.

Community
  • 1
  • 1
SoreThumb
  • 530
  • 1
  • 5
  • 17
  • It isn't a cache issue as far as I can tell. I even removed "window.history.back()" and I replaced it with a straight redirect to the first part of the form (which should, but isn't, displaying the data saved in the SESSION variables). – AKor May 05 '11 at 19:36
  • @Sennheiser You're saying you tried refreshing the page and doing that link redirect to see if the page will load the data from the session? Though I wouldn't use session to load submitted data, I'll keep in line with your desire: Have you tried a third page that will load the session and will do the same, using code snippets from page #1? I would suppose it's possible that the data is being overwritten, or not loaded when the page is first loaded due to some "SESSION" existence assumption, or SESSION loading. – SoreThumb May 05 '11 at 20:11
0

#1 of SoreThumb's answer is what I suggested might be happening in my comment.

But if that is the case, it's certainly not irrelevant.

You can tell the browser not to cache it when it is first loaded.

See this related question, this tutorial and this thread.

Community
  • 1
  • 1
jedwards
  • 29,432
  • 3
  • 65
  • 92