By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,932 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,932 IT Pros & Developers. It's quick & easy.

Saving form values between page loads

P: 1
I'm teaching myself PHP with limited success. I'm using PHP5 and dreamweaver.

I have a basic drop down menu on a form.

If the user forgets to enter some data I send him back to the form. What I need help with is how to show what was selected the first time so that he doesn't have to reenter all data just the missing stuff.

I'm really new at this and could use some direction.

Thanks

Here's the code

[PHP]<?php session_start();
$number_drivers = '';
$number_vehicles = '';
$error = array();
// read somewhere to set session variables to nothing at beginning
$_SESSION['number_drivers'] = $number_drivers;
$_SESSION['number_vehicles'] = $number_vehicles;

if (array_key_exists('step_two', $_POST)) {
// continue to step two button clicked
// get input and check for errors

if (empty($_POST['number_drivers'])) {
$error['number_drivers'] = 'Please enter the number of drivers';
}
if (empty($_POST['number_vehicles'])) {
$error['number_vehicles'] = 'Please enter the number of vehicles';
}

if (!$error) {
// store input to session variables

$_SESSION['number_drivers'] = $_POST['number_drivers'];
$_SESSION['number_vehicles'] = $_POST['number_vehicles'];

// go to step two
session_write_close();
header('Location: http://localhost/btracy_auto_quotes_2.php');
exit;
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title></title>
<link href="CssFiles/tracy.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
#auto1_main {position:absolute;
width:713px;
height:399px;
z-index:1;
left: 175px;
top: 20px;
}
.style6 {font-size: 12px}
.left_info {font-size: small}
.style1 {font-size: 14px}

.style7 {font-size: x-small}
-->
</style>
</head>

<body>
<div id="auto1_main">
<table width="700" border="0" align="center" cellpadding="0">
<tr>
<td><form id="auto_one" name="auto_one" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="680" border="0" align="center" cellpadding="0">
<tr>
<td colspan="3"><div align="center">
<h3>Auto Quote Step 1 of 2</h3>
</div>
</td>
</tr>
<tr>
<td width="402"><h3>About You </h3></td>
<td width="21">&nbsp;</td>
<td width="249">&nbsp;</td>
</tr>
<tr>
<td height="15"><?php if (isset($error['number_drivers'])) { ?>
<span class="warning"><?php echo $error['number_drivers']; ?></span><br />
<?php } ?>

<?php if (isset($error['number_vehicles'])) { ?>
<span class="warning"><?php echo $error['number_vehicles']; ?></span><br />
<?php } ?>

</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="style1">How many drivers are in your household? </td>
<td>&nbsp;</td>
<td>
<select name="number_drivers" id="number_drivers">
<option value="" selected="selected">Please Select</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
<span class="left_info">*</span> </td>
</tr>
<tr>
<td>&nbsp;<span class="style6">&nbsp; * Include yourself, your spouse, anyone of driving age in your household, and all other drivers of your vehicles.</span> </td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td class="style1">How many vehicles are you insuring? </td>
<td>&nbsp;</td>
<td><select name="number_vehicles" id="number_vehicles">
<option value="" selected="selected">Please Select</option>
<option value="9">Zero</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
<span class="left_info"> *</span>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<table width="682" border="0" align="center" cellpadding="0" id="step_two">
<tr>
<td width="39">&nbsp;</td>
<td width="228">&nbsp;</td>
<td width="407"><input name="step_two" type="submit" id="step_two" value="Continue to Step Two" /></td>
</tr>

</table>
</form>

</td>
</tr>
</table>
</div>
<p>&nbsp;</p>

</body>
</html>[/PHP]
Jul 31 '07 #1
Share this Question
Share on Google+
2 Replies


code green
Expert 100+
P: 1,726
The technique you are looking for is known as sticky forms.
I am not going to attempt to show you because the syntax is tricky.
But it is simple to understand.
Larry Ullman explains it well. Try this link
http://www.peachpit.com/articles/art...&seqNum=3&rl=1
Jul 31 '07 #2

pbmods
Expert 5K+
P: 5,821
Changed thread title to better describe the problem.
Jul 31 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.