We help IT Professionals succeed at work.

Caching form data

Frosty555 asked
I'm building a database website using PHP, Dreamweaver CS3, and MySQL. It's based pretty much on built on Dreamweaver's built in dynamic content templates. The pages follow CS3's style in that they post to themselves, and if they encounter a mysql error, then they die() with some relevant error message.

Problem is, after the page dies, if I hit the back button on the browser the form is blanked out. I can't go back to what I had previously typed in before hitting submit.

I've seen IE and FF "cache" the text you had typed in in previous forms in their history, so when you go back, the text is still there. Is there a way to turn this on on my page? Or does anyone know the reason why it isn't happening on mine?

Note: I'm not talking about restoring the values of the textboxes using PHP and the $_POST array when a page comes back with validation errors. I mean using the browser's back button and showing an older, cached form with text in it.
Watch Question



I don't think you can base yourself on the cache for the forms.
If you go back you should rebuild the form data yourself to be
sure is still there.

As far as I know Internet Explorer doesn't save everything in the forms.
If you go back the forms will be empty. It has autocomplete, but that
doesn't work for the text areas.

Top Expert 2004
The form cache is a client feature, not a server feature.  You can, of course, pre-populate all your form controls, but you have no way of knowing what the user entered until the form is POSTed.

One alternative is to not let the page die() on a database error.  Instead, record the error and send an alert, and allow the page to continue rendering.  That way, should be able to retain the POST information.


I know it isn't a 100% surefire anything, I was just looking for some way to preserve the data in a textfield... without having to go into every form I've created and add code to repopulate the data from the $_POST array.

I'm sure I've seen this before, even on my own websites, so not some website where they could be repopulating the data from the $_POST array. IE would remember some ir not all of the text you entered into a form. It might be that it is when you hit submit and the page encounters some error, like a 404 error, as opposed to actually reaching a new page...

Is there any kind of solution that doesn't involve me re-coding all my pages to fill default values into the form?
Top Expert 2004

As I said, the form cache is a client feature.  I'm sure you have seen it before, and I would suggest those sightings are examples of $_POST population, or your client retaining your entries.

The server has no way to know about the client's form cache, and would not have permission to access it if it did.  Unless the server receives $_POST data, or default data from another source (such as a database), you have no way to tap into previous entries.

Once you receive the $_POST data, you are left with programmatically inserting that data into the form controls.  


Damn. Well, I know how to do the $_POST repopulation, and I can remove the die() and replace it instead with an adding of data to an error property that I can display later in red in my form.

I just didn't want to edit every form I had made. Oh well.

Thanks everyone

Explore More ContentExplore courses, solutions, and other research materials related to this topic.