Google Answers Logo
View Question
 
Q: Remember HTML Form Values ( No Answer,   10 Comments )
Question  
Subject: Remember HTML Form Values
Category: Computers > Programming
Asked by: g8z-ga
List Price: $2.00
Posted: 03 Nov 2002 16:09 PST
Expires: 03 Dec 2002 16:09 PST
Question ID: 97642
Hello,

I have an HTML feedback form. After clicking submit, the HTML form
calls a PHP script to process the form. The PHP script checks to see
that every field is entered, and if not then an error message is
displayed and a "back" link is displayed.

The problem is that when I click on the "back" link (which uses
javascript:history.go(-1); ) the form contents are reset so the user
would have to retype everything rather than simply correcting the
error. I seem to have the same problem when using the "Back" button on
the web browser.

Here's a mock-up of a sample form to illustrate the problem:

http://www.windsorcap.com/test/

I have noticed, however, that this behavior does not occur on all the
servers on which I test the form. I operate another server, tufat.com,
and put *exactly* the same HTML form and PHP script (same version of
PHP is being used) at:

http://www.tufat.com/test/

On this server, I do not have the same problem. I.e., if you enter an
incomplete form, click submit, and then click the 'Back' link to
correct the form, the original form data is still present. This leads
me to believe that there is some server directive that is different.
E.g. a difference in the http.conf file in Apache or something.
They're both *nix servers, but I think that tufat.com is Linux and
windsorcap.com is Solaris. Both are using Apache, but I do not know
what flavor (both websites are hosted remotely).

I would like to know exactly why the form would save it's contents in
one case but not the other (Which server directive do I need to tweak
and how? Is there something in the server OS itself that has to be
changed? etc.)

Thanks,
Darren

Clarification of Question by g8z-ga on 07 Nov 2002 09:18 PST
I would like to know what HTTP headers are needed in order for the
form values to be remembered when I click the "Back" button on my
browser. If there is a global option that can be set in Apache (via
.htaccess or http.conf) then I would like to know what that option is
so that I don't have to manually modify the HTTP headers of every form
on my website.

I'm presuming that there must be some such option since every form on
windsorcap.com behaves the same way, and every form on tufat.com
behaves the same way, but they behave differently from each other.

Request for Question Clarification by mmastrac-ga on 15 Nov 2002 16:52 PST
I've taken a look at the HTTP headers your servers are sending.  Here
are the results:

On windsorcap:

HTTP/1.1 200 OK
Date: Sat, 16 Nov 2002 00:46:21 GMT
Server: Apache/1.3.26 (Unix) PHP/4.2.2 mod_gzip/1.3.19.1a
X-Powered-By: PHP/4.2.2
Set-Cookie: PHPSESSID=7fa885ec638276b1a27614bbe76258ca; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0,
pre-check=0
Pragma: no-cache
Connection: close
Content-Type: text/html

On tufat.com:

HTTP/1.1 200 OK
Date: Sat, 16 Nov 2002 00:47:35 GMT
Server: Apache/1.3.27 (Unix) FrontPage/5.0.2.2510 mod_jk/1.1.0
X-Powered-By: PHP/4.2.3
Connection: close
Content-Type: text/html

Have you configured PHP to send the no-cache headers?  Virtually every
no-cache header available has been set in its response!

Also, you are running PHP 4.2.2 on windsorcap and 4.2.3 on tufat.  If
you control the windsorcap, please do a grep for the following string
in your /etc directory like so:

grep "Cache-Control" `find`

It may be that one of your PHP configuration files is specifying these
cache parameters, even for content that is static.

Request for Question Clarification by mmastrac-ga on 15 Nov 2002 16:56 PST
Please note- you need to *remove* or override the following headers:

Expires: ...
Cache-Control: ...
Pragma: no-cache

Clarification of Question by g8z-ga on 15 Nov 2002 19:07 PST
I've tried overriding the HTTP headers by including header("...") at
the beginning of each document (having them auto-included into every
PHP file using the auto-prepend method described in one of the
comments), but I haven't been able to get it to work, so I suspect
that there's something overriding my attempt to manually configure the
headers - either Apache or PHP engine.

The problem exists even on pages that are plain HTML with .html
extension - so it seems that the PHP parser wouldn't touch such pages.

I'm aware of the difference in the headers, and I'm sure that's the
problem. But I need a quick way to rid all the no-cache parts of the
header.

But I'll scan for Cache-Control, as suggested, and report my findings.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Remember HTML Form Values
From: darthcow-ga on 03 Nov 2002 19:06 PST
 
Well, you're already using PHP, so the solution should be simple.
Instead of using the go back and fix it link, just create another copy
of the form with the proper variables inserted for the values of the
input field. Here's how you would do it on your sample form... just
replace the link with the text below.

<h2>Sample Form</h2>
For a successful form submission, you must type some text, and click
the checkbox.<br>
<form name=myForm action=processForm.php method=POST>
Type some text: <input type=text size=40 name=text value="<?php echo
$_POST['text'] ?>"><br>
Click here to indicate agreement with our policy: <input type=checkbox
name=checkbox value=Yes><br><br>
<input type=submit name=submit value=submit>
</form>
Subject: Re: Remember HTML Form Values
From: darthcow-ga on 03 Nov 2002 19:07 PST
 
Just remember to remove the line breaks within the php - "<?php echo
$_POST['text'] ?>" doesn't work but "<?php echo $_POST['text'] ?>"
does (if even that shows up right :P).
Subject: Re: Remember HTML Form Values
From: thisisjeff-ga on 04 Nov 2002 00:55 PST
 
windsorcap.com has client-side caching disabled.  tufat.com does not.

Each time a page on windsorcap.com is accessed, the browser is
specifically instructed to not save it in the user's cache.  This
forces the browser to reload the page from the server, and has the
side effect of resetting the form fields.  tufat.com does not impose
this browser restriction.

I'm not sure what Apache settings you'd want to modify, specifically,
to fix this.  This is happening because windsorcap.com's server is
sticking a "Expires: (date)" header, along with the standard
"Content-type: text/html" header, at the top of each page it serves. 
Search for options that include "expir" or "cach" in their names, and
compare and contrast between the two servers.

Hope this helps.
Subject: Re: Remember HTML Form Values
From: g8z-ga on 04 Nov 2002 02:40 PST
 
hi, 

Thanks for the feedback. Since reading these comments and doing some
tinkering, I've figured out what the HTTP headers need to be set to in
order for the form caching to work correctly.

However, I have a lot of forms in the project I'm working on. Is there
some way - e.g. through an .htaccess directive of some sort - to set
the HTTP headers automatically for all files in a site?

- Darren
Subject: Re: Remember HTML Form Values
From: mward-ga on 04 Nov 2002 04:52 PST
 
I read somewhere that if you use the same file in your POST command to
process the data as you do to gather the data, that the values won't
be lost should an error be returned.

I can't confirm the answer right now (will look more indept later) but
this is an avenue you might want to try.
Subject: Re: Remember HTML Form Values
From: g8z-ga on 04 Nov 2002 09:11 PST
 
I've tried that (making the same script process the form - ie the form
action is to $PHP_SELF), but I get the same result. I'm certain that
it must be the HTTP headers. The question that I have is just: how can
I globally tweak the server (e.g. through .htaccess) to change the
HTTP headers of every page?
- Darren
Subject: Re: Remember HTML Form Values
From: alexd-ga on 04 Nov 2002 10:02 PST
 
Here's what I've put in my .htaccess file:
php_value auto_prepend_file /home/webfiles/www.yourdomain.com.com/header.php

And in that PHP file I set several headers, some cookies, etc...

I hope this helps !
Subject: Re: Remember HTML Form Values
From: g8z-ga on 04 Nov 2002 20:58 PST
 
hi all,

Thanks for all the wonderful feedback. After poring through the Apache
and HTTP 1.1 documentation, I have decided to try manually setting the
HTTP headers to solve the problem. However, after changing the
"Pragma:..." and "Cache-control:..." headers quite a bit by using
PHP's header(...) function, I still can't seem to get the desired
result. I'm not sure if the server is overriding my headers (seems
unlikely) or if I'm just a lame-o and can't figure out what the right
combination of headers is to make the form values cache (more likely).

Can anyone suggest a combination of HTTP headers that will work?

Thanks,
Darren
Subject: Re: Remember HTML Form Values
From: tox-ga on 08 Nov 2002 20:02 PST
 
Another method to save these form values would be to use javascript
cookies.  If you are interested in this method, post the code for the
html form and I'll write the script for you as the answer.
Also, have you considered running Apache 2.0?
I have it running right now and the caching is fine.

-Tox-ga
Subject: Re: Remember HTML Form Values
From: g8z-ga on 09 Nov 2002 13:03 PST
 
yes, Javascript cookies would be a way to remember the form values. As
would PHP or Perl cookies with something like value=<? print
$fieldValue; ?> as each field's value property.

The problem with these approaches is that they are very laborious, and
do not provide a general, global solution to the problem. The cookie
would have to be inserted into every field of every form on my website
(we're taking about dozens if not hundreds of forms, from a few fields
to dozens of fields each). Not to mention things like drop-down lists,
checkboxes and radio buttons, for which code like

<? 
if ( $itemAChecked ) print " selected ";
else if ...
?>

would be needed to ensure that the appropriate checkbox or radio
button is selected when the user clicks the "Back" button. Obviously,
in a form with dozens of checkboxes, this would take a long time.

If there's a simple way to enable the caching of Form values as a
global Apache directive, either by editing http.conf or via a command
in the .htaccess file, that would really be the solution that I'm
looking for. I'm absolutely certain that there must be some way to do
it given the nature of the two examples (one from tufat.com and one
from windsorcap.com).

Changing the HTTP headers of every form would be possible, too, since
I could easily include the headers into each HTML page by templating
them (e.g. in Dreamweaver), or by using php_value auto_prepend_file,
as one user suggested. While this would be a less-than-ideal
workaround, I would consider that an acceptable solution to my query.
But I've tried several combinations of HTTP headers that would appear
to affect page caching, and I haven't found a combination that works
yet.

- Darren

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy