Jump to content


Photo

$_POST variables lost when hitting the browsers back button


  • Please log in to reply
6 replies to this topic

#1 Seamless

Seamless
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 05 October 2006 - 07:48 AM

Hi,

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

Seamless


#2 mjlogan

mjlogan
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 05 October 2006 - 08:20 AM

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

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


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

#3 Seamless

Seamless
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 05 October 2006 - 04:10 PM

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 = " ";
	if(!$_POST[customer]){
		$form_error .= "<p>A customer is required.</p>";
		$x++;
	}
	if(!$_POST[cust_contact]){
		$form_error .= "<p>A customer contact is required.</p>";
		$x++;
	}
	if($_POST[cust_contact_email]){
		if(!validate_email($_POST[cust_contact_email])){
			$form_error .= "<p>A valid email address must be entered for the customer contact email.</p>";
			$x++;
		}
	}
	if(!$_POST[part_des]){
		$form_error .= "<p>A Part Description is required.</p>";
		$x++;
	}
    if(!$_POST[change_des]){
		$form_error .= "<p>Details of the change is required.</p>";
		$x++;
	}
	if(!$_POST[effects_des]){
		$form_error .= "<p>Effects of the change is required.</p>";
		$x++;
	}
	if($_POST[action1] == "0" or $_POST[action_by1] == "0" or $_POST[custom_action1] == ""){
		$form_error .= "<p>At least 1 action must be selected.</p>";
		$x++;
    }
	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> 
			</form>";
    }else{
// 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.


#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 05 October 2006 - 04:15 PM

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?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 05 October 2006 - 06:07 PM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 05 October 2006 - 08:34 PM

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.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#7 Seamless

Seamless
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 06 October 2006 - 08:54 AM

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

Seamless




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users