Jump to content


This topic is now archived and is closed to further replies.


$_POST variables lost when hitting the browsers back button

Recommended Posts


I wonder if any one can help me..

I am developing a drawing register for the company i work for on our intranet. We recently got a new server (Windows 2003) and IIS6, so i installed PHP and MySQL on it.

When a user fills in a web form (i'll call it form.php) on the intranet i make the form post the variables to another file (i'll call this one form-parse.php) which parses the data. If they have not entered the required information i display the form errors on the form-parse.php telling them to go back to the original form and fill in the required fields.

My problem is that when a user clicks the back button on the browser (from form-parse.php to form.php) after getting an error for not entering data in a required field the $_POST variables are lost and they have to fill in the complete form again.

I have included value="<? echo $_POST[variable1];?>" for each of the relevant <input> tags in the original form.

I have managed to work around this for now by displaying a "Back" button under the errors which posts hidden post data back to form.php, but this is quite long winded and in my opinion unneccessary.

I have not come across this problem before after using a few different web hosting companies....

Could it be a setting in the php.ini file or is this something to do with the server settings and this post is in the wrong forum completely.

Thanks in advance


Share this post

Link to post
Share on other sites
Is form-parse.php included in form.php?

Because from what I understand so far, you have a form on form.php that posts some variables to form-parse.php that will work find using $_POST but then it's a simple <a href=" link back to form.php if errors have occured. Which $_POST will then not work, as

[b]HTTP POST variables: $_POST
An associative array of variables passed to the current script via the HTTP POST method.[/b]

which you are not doing, or have I missed something.

Share this post

Link to post
Share on other sites
cheers for the reply.

there are 2 seperate files:
1. form.php
2. form-parse.php

your part right -  a form on form.php posts variables to form-parse.php.

I think you are a little confused about the next part, maybe my explaination wasn't very good...

Here's the script my form posts its variables to - form-parse.php

$x = 0;
    $form_error = " ";
$form_error .= "<p>A customer is required.</p>";
$form_error .= "<p>A customer contact is required.</p>";
$form_error .= "<p>A valid email address must be entered for the customer contact email.</p>";
$form_error .= "<p>A Part Description is required.</p>";
$form_error .= "<p>Details of the change is required.</p>";
$form_error .= "<p>Effects of the change is required.</p>";
if($_POST[action1] == "0" or $_POST[action_by1] == "0" or $_POST[custom_action1] == ""){
$form_error .= "<p>At least 1 action must be selected.</p>";
if($x > 0){
echo "<p><b>Please correct the errors below.</b></p><br />";
echo "$form_error";
echo "<form action=\"create-change.php\" method=\"post\">
<input type=\"hidden\" name=\"customer\" value=\"$_POST[customer]\" />
<input type=\"hidden\" name=\"cust_contact\" value=\"$_POST[cust_contact]\" />
<input type=\"hidden\" name=\"cust_contact_email\" value=\"$_POST[cust_contact_email]\" />
<input type=\"hidden\" name=\"part_des\" value=\"$_POST[part_des]\" />
<input type=\"hidden\" name=\"cust_part_number\" value=\"$_POST[cust_part_number]\" />
<input type=\"hidden\" name=\"bwm_num\" value=\"$_POST[bwm_num]\" />
<input type=\"hidden\" name=\"samples\" value=\"$_POST[samples]\" />
<input type=\"hidden\" name=\"cost_charge\" value=\"$_POST[cost_charge]\" />
<input type=\"hidden\" name=\"cust_order_num\" value=\"$_POST[cust_order_num]\" />
<input type=\"hidden\" name=\"total_cost\" value=\"$_POST[total_cost]\" />
<input type=\"hidden\" name=\"change_des\" value=\"$_POST[change_des]\" />
<input type=\"hidden\" name=\"effects_des\" value=\"$_POST[effects_des]\" />
<input type=\"hidden\" name=\"delivery\" value=\"$_POST[delivery]\" />
<input type=\"hidden\" name=\"compatibility\" value=\"$_POST[compatibility]\" />
<input type=\"hidden\" name=\"action1\" value=\"$_POST[action1]\" />
<input type=\"hidden\" name=\"custom_action1\" value=\"$_POST[custom_action1]\" />
<input type=\"hidden\" name=\"action_by1\" value=\"$_POST[action_by1]\" />
<input type=\"hidden\" name=\"action2\" value=\"$_POST[action2]\" />
<input type=\"hidden\" name=\"custom_action2\" value=\"$_POST[custom_action2]\" />
                <input type=\"hidden\" name=\"action_by2\" value=\"$_POST[action_by2]\" />
                <input type=\"hidden\" name=\"action3\" value=\"$_POST[action3]\" />
<input type=\"hidden\" name=\"custom_action3\" value=\"$_POST[custom_action3]\" />
<input type=\"hidden\" name=\"action_by3\" value=\"$_POST[action_by3]\" />
                <input type=\"hidden\" name=\"action4\" value=\"$_POST[action4]\" />
<input type=\"hidden\" name=\"custom_action4\" value=\"$_POST[custom_action4]\" />
<input type=\"hidden\" name=\"action_by4\" value=\"$_POST[action_by4]\" />
                <input type=\"hidden\" name=\"action5\" value=\"$_POST[action5]\" />
<input type=\"hidden\" name=\"custom_action5\" value=\"$_POST[custom_action5]\" />
<input type=\"hidden\" name=\"action_by5\" value=\"$_POST[action_by5]\" />
                <input type=\"hidden\" name=\"action6\" value=\"$_POST[action6]\" />
<input type=\"hidden\" name=\"custom_action6\" value=\"$_POST[custom_action6]\" />
<input type=\"hidden\" name=\"action_by6\" value=\"$_POST[action_by6]\" />
                <input type=\"hidden\" name=\"action7\" value=\"$_POST[action7]\" />
<input type=\"hidden\" name=\"custom_action7\" value=\"$_POST[custom_action7]\" />
<input type=\"hidden\" name=\"action_by7\" value=\"$_POST[action_by7]\" />
                <input type=\"hidden\" name=\"action8\" value=\"$_POST[action8]\" />
<input type=\"hidden\" name=\"custom_action8\" value=\"$_POST[custom_action8]\" />
<input type=\"hidden\" name=\"action_by8\" value=\"$_POST[action_by8]\" />
                <p><input type=\"submit\" value=\"Back\" /></p>
<p><b>NOTE: </b>If you don't use the Back button above, you will have to fill out the change form again!</p>
// if no errors process the data

You can see that i've added a form with hidden fields so that when the back button on the form is clicked it posts the data back to the original form on form.php

What i'm trying to get at is - I shouldn't need to have that form with hidden fields on form-parse.php because when you hit the back button on the browser the post variables should appear back in the form on form.php like it does on the website i have which is not on an intranet.

Share this post

Link to post
Share on other sites
you could start a session and use session variables to auto-fill your forms instead.

i think...

i mean, in theory that should work, but browsers usually use a cached version of the webpage when you hit the back button.  maybe you could put a header in the form.php that will always require it to be a fresh load, so the session method will work?

Share this post

Link to post
Share on other sites
I think that javascript (Ajax) would be the best way to do this. Do the field validation before sending it to the php file.

Share this post

Link to post
Share on other sites
Yeah, kind of hard to reference POST variables when they were not posted to the page (i.e. when you hit the back button. I would use the suggestion above of using SESSION variables. To make it simple, just add the POST array to a single session variable.

To also make things easier, you could make the form page and the form processing page the same. When the form is posted, first process the data, if there are errors present the user with the errors and re-display the form with their previous data.

Share this post

Link to post
Share on other sites
Thanks for your suggestions i am using a session for the users to login anyway so that is probably the best solution. Although i kinda wanted to solve the problem rather than create a workaround.

But you just triggered my memory when you mentioned headers i had not included

header("Cache-control: private");

beneath session_start(); at the top of the two pages, so i have added it and that has solved the problem!

I don't know why i did not include it as i usually do. A lesson learnt for me now, i won't be making that mistake again!

Thanks for your help


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.