![]() |
|
![]() | ||
|
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 | |
| |
| |
| |
|
![]() | ||
|
There is no answer at this time. |
![]() | ||
|
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 |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |