Hi,
I am having problems updating session variables when using the back button.
page1.php sets the first set of session variables as expected, I then use header to go to page2.php, where more session variables are set. Once page2.php has been submitted, I use header to go to success.php, which prints the session variables set in page1.php and page2.php. However, if I decide to go back to page2.php and make changes, then go back to page1.php then forward to page2.php, the session variables are not updated. They are only updated if I press the submit button on page2.php. I was wondering how to solve this problem. I do acknowledge that the session variables are doing what they are supposed to be doing, but I just need them to update.
here is the code for page1.php:
<?php
session_start();
$_SESSION['clientname'] = (isset($_POST['name']) ? $_POST['name'] : (isset($_SESSION['clientname']) ? $_SESSION['clientname'] : '')); //name
$_SESSION['clientcompany'] = (isset($_POST['company']) ? $_POST['company'] : (isset($_SESSION['clientcompany']) ? $_SESSION['clientcompany'] : '')); //company
$N = '';
$C = '';
if(isset($_POST['submit']))
{
//process the radio buttons
//process the radio buttons
if(isset($_POST['name']))
$N = $_POST['name'];
else
$N = false;
if(isset($_POST['submit']))
//process the text-input field
if(isset($_POST['company']))
$C = $_POST['company'];
else
$C = false;
//echo '<p>Other Choice<br />';
//if all entries okay, process information, display message and exit script.
if($N && $C)
{
header("Location: page2.php");
}
}
?>
<html>
<head>
<title>Page 1</title>
<head>
</head>
<body>
<?php
echo '<h2>Page 1</h2>';
echo '<form name="Form1" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
//display text-input label for name
if(isset($_POST['submit']) && !$N)
echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
else
echo '<p>Please enter Name [required]<br />';
//display the text-input field
echo '<input type="text" name="name" maxlength="60" size="60"
value="' . $_SESSION['clientname'] . '" /></p>';
if(isset($_POST['submit']) && !$C)
echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
else
echo '<p>Please enter Company [required]<br />';
//display the text-input label for company
echo '<input type="text" name="company" maxlength="60" size="60"
value="' . $_SESSION['clientcompany'] . '" /></p>';
echo '<p><input type="submit" name="submit" value="submit"></p>';
echo '</form>';
echo '</body>';
echo '</html>';
?>
here is the code for page2.php
<?php
session_start();
$_SESSION['otherchoice'] = (isset($_POST['choice']) ? $_POST['choice'] : (isset($_SESSION['otherchoice']) ? $_SESSION['otherchoice'] : '')); //radio
$_SESSION['textchoice'] = (isset($_POST['txt']) ? $_POST['txt'] : (isset($_SESSION['textchoice']) ? $_SESSION['textchoice'] : '')); //text
//$_SESSION['otherchoice'] = $_POST['choice']; //radio
//$_SESSION['textchoice'] = $_POST['txt']; //text
$OC = '';
if(isset($_POST['submit']))
{
//process the radio buttons
if(isset($_POST['choice']))
$OC = $_POST['choice'];
else
$OC = false;
if($OC == 'yes' && ($_SESSION['textchoice'] == ''))
$OC = false;
else
$OC = $_POST['choice'];
//echo '<p>Other Choice<br />';
//if all entries okay, process information, display message and exit script.
if($OC)
{
header("Location: success.php");
}
}
?>
<html>
<head>
<title>Page 2</title>
<head>
<script type="text/javascript">
function make_blank()
{
document.theform.txt.value ="";
}
</script>
<script type="text/javascript">
function make_blank2()
{
document.theform.txt2.value ="";
}
</script>
</head>
<body>
<?php
echo '<h2>Page 2</h2>';
echo '<form name="theform" action="' . $_SERVER['PHP_SELF'] . '" method="POST">';
if(isset($_POST['submit']) && !$OC)
echo '<p><font color="red" size="+1"><b>Please Specify</b></font><br />';
else
echo '<p>What colour would you like? [required]<br />';
//if($OC == 'red')
if ($_SESSION['otherchoice'] == 'red')
echo '<input type="radio" name="choice" value="red" checked onClick="make_blank();this.form.txt.disabled=true;"/>red <br />';
else
echo '<input type="radio" name="choice" value="red" onClick="make_blank();this.form.txt.disabled=true;"/>red <br />';
//if($OC == 'green')
if ($_SESSION['otherchoice'] == 'green')
echo '<input type="radio" name="choice" value="green" checked onClick="make_blank();this.form.txt.disabled=true;"/>green <br />';
else
echo '<input type="radio" name="choice" value="green" onClick="make_blank();this.form.txt.disabled=true;"/>green <br />';
//if($OC == 'orange')
if ($_SESSION['otherchoice'] == 'orange')
{
echo '<input type="radio" name="choice" value="orange" checked onClick="make_blank();this.form.txt.disabled=true;"/>orange <br />';
}else{
echo '<input type="radio" name="choice" value="orange" onClick="make_blank();this.form.txt.disabled=true;"/>orange <br />';
}
//if($OC == 'other')
if ($_SESSION['otherchoice'] == 'other')
echo '<input type="radio" name="choice" value="other" onClick="this.form.txt.disabled=false;this.form.txt.focus()"checked />Other...Please specify ';
else
echo '<input type="radio" name="choice" value="other" onClick="this.form.txt.disabled=false;this.form.txt.focus();"/>Other...Please specify ';
echo '<input disabled type="text" name="txt" value="' . $_SESSION['textchoice'] .'" />';
echo '<br /><input type="submit" name="submit" value="submit">';
echo '<input type="button" value="back" onclick="history.back(-1)" />';
echo '</form>';
echo '</body>';
echo '</html>';
?>
here is the code for success.php :
<?php
session_start();
echo 'What colour would you like? : ';
if ($_SESSION['otherchoice'] == "other"){
echo "other";
echo "<br>";
echo 'Other information is : ';
echo $_SESSION['textchoice'];
echo "<br>";}
else {
echo $_SESSION['otherchoice'];
echo "<br>";}
echo "<br>";
echo 'Client Name : ';
echo $_SESSION['clientname'];
echo "<br>";
echo 'Client Company : ';
echo $_SESSION['clientcompany'];
echo "<br>";
?>
Any help would be greatly apprecitaed, Thanks!