I've used a construct similar to this one.
The form page itself, say
form.php:
PHP Code:
<?PHP
/* Error array, keyed on input field name. */
$errors = array();
/*
* ... form validation ...
*/
if (!preg_match(/[0-9A-Za-z]@.+.[0-9A-Za-z]+$/, @$_POST['email']))
$errors['email'] = 'You must supply a valid e-mail address.';
if (count($errors) > 0)
require_once('form.fill.inc');
else
require_once('form.save.inc');
?>
The
form.fill.inc is the actual page that produces the form itself. It will have constructs similar to say
PHP Code:
<td class="label">E-mail address:</td>
<td class="input"><?PHP
echo '<input type="text" name="email" value="',
htmlentities(@$_POST['email'], ENT_COMPAT, 'UTF-8'),
'" maxlength="128">';
if (array_key_exists('email', $errors))
echo '<span class="error">',
htmlentities($errors['email'], ENT_COMPAT, 'UTF-8'),
'</span>';
?></td>
Obviously you should use a helper function for each type of input field (one-line text input, multiline text input, table of radio buttons) instead of open-coding it like I did above. Just as an example, consider
PHP Code:
<?PHP
function textinput($name, $class = '', $maxlength = 0) {
global $errors;
echo '<input type="text" name="', $name;
if (strlen($class) > 0)
echo '" class="', $class;
echo '" value="', htmlentities(@$_POST[$name], ENT_COMPAT, 'UTF-8');
if ($maxlength > 0)
echo '" maxlength="', $maxlength;
if (array_key_exists($name, $errors))
echo '"><span class="error">',
htmlentities($errors[$name], ENT_COMPAT, 'UTF-8'),
'</span>';
else
echo '">';
}
?>
<td class="label">E-mail address:</td>
<td class="input"><?PHP textinput('email'); ?></td>
When the form is filled properly, the actual processing is done by the
form.save.inc page.