A good way of displaying fields which haven't been filled out is to use a session to temporaily store the form information. here is the logic behind it - it has worked well for me and I use it in my site at the moment.
First we have a form template:
form_template.php
<?php
if (isset($_GET['errors'])) {
/* this means our form has been rejected due to errors
a session should have been created in the form processor
and we will start it here
*/
session_start();
/* load the form data from the session into a local variable */
$form_data = @$_SESSION['form_data'];
/* now we've collected the form data we can delete the session */
session_destroy();
}
/* below is the template we use the @ suppression operator to prevent
undeclared variable notices from disp[lay if there weren't any errors */
?>
<html>
<head>
<title>Form Redirection Example</title>
</head>
<body>
<form method="post" action="form_processor.php">
<p><?php echo(@$form_data['err']) ?></p>
<table>
<tr>
<td></td>
<td><?php echo(@$form_data['string']['err']) ?></td>
</tr>
<tr>
<td>Enter some text here:</td>
<td><input type="text" name="string"
value="<?php echo(@$form_data['string']['value']) ?>" />
</td>
</tr>
<tr>
<td></td>
<td><?php echo(@$form_data['number']['err']) ?></td>
</tr>
<tr>
<td>Enter some text here:</td>
<td><input type="text" name="number"
value="<?php echo(@$form_data['number']['value']) ?>" />
</td>
</tr>
<tr>
<td><input type="submit" value="Submit Form" /></td>
</tr>
</table>
</form>
</body>
</html>
We also need a processing script:
form_processor.php
<?php
$form_data = Array();
$error = false;
/* check for data */
if (! posted('string', 'number')) {
$form_data['err'] = 'Invalid data format.';
$error = true;
} else {
// assume magic quotes is off
$string = $_POST['string'];
$number = $_POST['number'];
if ($string == '' ) { // its been left blank
$form_data['string']['err'] = 'This field is required.';
$error = true;
}
if ($number == '') { // its been left blank
$form_data['number']['err'] = 'This field is required.';
$error = true;
} else if (! is_numeric($number)) { // not a number
$form_data['number']['err'] = 'This field must be a number.';
$error = true;
}
}
/* check if any errors occured */
if ($error) {
if (! isset($form_data['err'])) {
$form_data['string']['value'] = htmlspecialchars($string);
$form_data['number']['value'] = htmlspecialchars($number);
}
// force the session to use the query string
ini_set('session.use_cookies', '0');
session_start();
/* store the errors and data tempraily in the session */
$_SESSION['form_data'] = $form_data;
/* go back to the form */
header('Location: form_template.php?errors=1&' . SID);
} else {
/* we could redirect to the success page here */
echo('success');
}
/* helper function to test variables were submitted via HTTP POST */
function posted()
{
$num_args = func_num_args();
$args = func_get_args();
for($i = 0; $i < $num_args; $i++) {
if (! isset($_POST[$args[$i]])) {
return false;
}
}
return true;
}
?>
You can see a working example here, hope it helps:
http://adam.codedv.com/eaxmples/form_redirect/form_template.php