I have crated a web form that sends it's data to a PHP script for handling.
I would like to know how I can refresh the form, KEEPING the good values and deleting the bad values (ones that did not pass validation).
Right now, if someone submits the form, and there are errors in it, the form will reload, but ALL of the form fields will be empty.
I would like to be able to return to the form, display an error message, and have all of the data the person entered to still be in the form (except for the bad fields).
Any help would be appreciated.
Todd
Web forms and PHP
Moderator: General Moderators
You could do something like this...
Code: Select all
<?
function check_form($form_data) {
$errors = NULL;
$required = array('fromName','email');
foreach ($required as $key => $val) {
$form_dataї$val] = trim($form_dataї$val]);
if ( ($form_dataї$val] == NULL) || ($form_dataї$val] == '') ) {
$errors .= "$val~";
}
}
return $errors;
}
function check_email($email) {
if(!ereg("^ї_\.''0-9a-z-]+@(ї0-9a-z]ї0-9a-z-]+\.)+їa-z]{2,3}$",$email)) {
$error = "bad email";
return $error;
} else {
return NULL;
}
}
display_form($form_data, $form_errors) {
echo '<form name="form" action="$PHP_SELF" method="post">'
if ($mail_error != '') {
echo "<font color="red"><center><h3>Email Error</font></h3><br>";
echo $mail_error."<br>";
}
if ($form_errors != '') {
echo "<font color="red"><center><h3>Please complete the required fields.</font></h3><br>";
}
if (ereg('fromName', $form_errors)) { echo '<font color="red">'; }
?>
<b><font color="#0033FF">*</font>Name:</b></td>
<td colspan="1" align="left">
<input name="form_dataїfromName]" type="text" size="50"
<?
if (isset($form_dataї'fromName'])) {
echo "value="".$form_dataї'fromName']."">";
} else {
echo ">";
}
if (ereg('email', $form_errors)) { echo '<font color="red">'; }
?>
<b><font color="#0033FF">*</font>Email:</b></td>
<td colspan="1" align="left">
<input name="form_dataїemail]" type="text" size="50"
<?
if (isset($form_dataї'email'])) {
echo "value="".$form_dataї'email']."">";
} else {
echo ">";
}
echo "</form>";
}
// Main
if (isset($_POSTї'form_data']ї'submit'])) {
$form_data = $_POSTї'form_data'];
$form_errors = check_form($form_data);
$form_errors .= check_email($form_dataї'email']);
if ($form_errors != '') {
display_form($form_data, $form_errors, NULL);
} else {
// do something...
}
} else {
display_form($form_data, NULL, NULL);
}
?>