Help with PHP form needed
Posted: Mon Jun 30, 2003 4:07 pm
I have this PHP form. The validation itself works but I need the form to repoplulate when a user missed a required field (radio buttons and checkboxes) Also a summary of the inputs should show up without the form being visible again after they sumbit, and the users favourite color as the background. I'm kind of new at this so the code might not be as clean as many pro's here write. Be easy on me. If possible, please fill in any missing code that I should use to make this work. I ran out of ideas.
mod_edit:
Code: Select all
<html>
<head>
<title>PHP Form</title>
</head>
<body bgcolor="#000000" alink="#000000" vlink="#0000ff">
<table border="1" align="center" bgcolor="#ccccff" cellpadding="5">
<tr>
<td>
<h4><font color="#000000">PHP Form</font></h4>
<hr width="600">
<?php
//session_start();
if ($REQUEST_METHOD == "POST") {
$errors = validate_data($f_name, $l_name, $age, $phone, $mail_list);
if ($errors) {
for ($i=0; $i < count($errors); $i++)
print $errors[$i];
}else{
print thank_user();
}
}
?>
<form action="<?= $PHP_SELF ?>" method="post" name="output">
<?php
function validate_data($f_name, $l_name, $age, $phone, $mail_list) {
if ($f_name == "") {
$errors[]="<font color="#ff0000">You did not enter your first name.</font><br>";
}
if ($l_name == "") {
$errors[]="<font color="#ff0000">You did not enter your last name.</font><br>";
}
if ($age == "") {
$errors[]="<font color="#ff0000">You did not enter your age.</font><br>";
}
elseif (!is_numeric($age)) {
$errors[]="<font color="#ff0000">Age must be numeric.</font><br>";
}
elseif ($age < 18 || $age > 110) {
$errors[]="<font color="#ff0000">Age entered must be between 18 and 110.</font><br>";
}
$len = strlen($phone);
if ($len < 11) {
$errors[]="<font color="#ff0000">You must enter a phone number with at least 11 characters.</font><br>";
}
if ($mail_list == "Yes" || $mail_list == "No") {
}else{
$errors[]="<font color="#ff0000">You must specify if you would like to be contaced by e-mail.</font><br>";
}
return $errors;
} //Close validate_data block.
function thank_user() {
print ("<h3><center>Thank you for visiting our site $title $l_name.</center></h3>");
print ("<h4>Here is a summary of the information you provided:</h4>");
print ("Your name: $f_name $l_name<br>");
print ("Age: $age<br>");
print ("Gender: $gender<br>");
print ("Address: $address<br>");
print ("Province: $province<br>");
print ("Phone Number: $phone<br>");
print ("E-mail: $email<br>");
print ("Time we can contact you: $contact<br>");
print ("Are you on our mailing list? $mail_list<br>");
print ("You rated our site as: $rate<br>");
print ("Your comments were: $comments");
} //Close thank_user block.
include ("header.php"); //Header file.
//function display_form() { //Open display_form function.
print ("<table border="0" cellpadding="3">");
print ("<tr><td colspan="2">Fields with a <font color="#ff0000">*</font> are required.</td></tr>");
print ("<tr>");
print ("<td align="right">");
print ("Title:");
print ("</td>");
print ("<td> ");
print ("<input type="radio" name="title" value="Mr.">Mr.");
print ("<input type="radio" name="title" value="Mrs.">Mrs.");
print ("<input type="radio" name="title" value="Miss">Miss");
print ("<input type="radio" name="title" value="Ms">Ms");
print ("<input type="radio" name="title" value="Dr.">Dr.");
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td align="right">");
print ("<font color="#ff0000">* </font>First Name:");
print ("<td colspan="2">");
print ("<input type="text" name="f_name" maxlength="19" size="20" value='$f_name'>");
print ("</td>");
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td align="right">");
print ("<font color="#ff0000">* </font>Last Name:");
print ("<td colspan="2">");
print ("<input type="text" name="l_name" maxlength="19" size="20" value='$l_name'>");
print ("</td>");
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td align="right">");
print ("<font color="#ff0000">* </font>Age:");
print ("<td colspan="2">");
print ("<input type="text" name="age" maxlength="3" size="5" value='$age'>");
print ("</td>");
print ("</td>");
print ("</tr>");
print ("<tr>");
print ("<td align="right">");
print ("Gender:");
print ("<td colspan="2">");
print ("<input type="radio" name="gender" value="Male">Male");
print ("<input type="radio" name="gender" value="Female">Female");
print ("</td>");
print ("</td>");
print ("</tr>");
print ("<tr>
<td align="right">
Address:
<td colspan="2">
<input type="text" name="address" maxlength="19" size="20" value='$address'>
</td>
</td>
</tr>");
print ("<tr>
<td align="right">
Province:
<td colspan="2">
<input type="text" name="province" maxlength="19" size="20" value='$province'>
</td>
</td>
</tr>");
print ("<tr>
<td align="right">
<font color="#ff0000">* </font>Telephone Number:
<td colspan="2">
<input type="text" name="phone" maxlength="19" size="20" value='$phone'>
</td>
</td>
</tr>");
print ("<tr>
<td align="right">
E-mail Address:
<td colspan="2">
<input type="text" name="email" size="20" value='$email'>
</td>
</td>
</tr>");
print ("<tr>
<td align="right">
When can we contact you?
<td>
<input type="checkbox" name="contact" value="Morning">Morning
<input type="checkbox" name="contact" value="Afternoon">Afternoon
<input type="checkbox" name="contact" value="Evening">Evening
</td>
</td>
</tr>");
print ("<tr>
<td align="right">
<font color="#ff0000">* </font>Would you like to be on our mailing list?
<td colspan="2">
<input type="radio" name="mail_list" value="Yes">Yes
<input type="radio" name="mail_list" value="No">No
</td>
</td>
</tr>");
print ("<tr>
<td align="right">
What is your favourite color?
<td colspan="2">
<select name="color" size="1">
<option>Pick a color...</option>
<option>--------------------</option>
<option>Red</option>
<option>Green</option>
<option>Blue</option>
<option>Orange</option>
<option>Pink</option>
<option>Yellow</option>
</select>
</td>
</td>
</tr>");
print ("<tr>
<td align="right">
How would you rate our site?
<td colspan="2">
<input type="radio" name="rate" value="Excellent">Excellent
<input type="radio" name="rate" value="Good">Good
<input type="radio" name="rate" value="Needs Work">Needs Work
</td>
</td>
</tr>");
print ("<tr>
<td colspan="2">
</td>
</tr>");
print ("<tr>
<td colspan="2" align="left">
Please enter any comments here:<br>
<textarea rows="5" cols="50" name="comments"></textarea>
</td>
</tr>");
print ("<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit Form"> <input type="reset" value="Clear Form">
</td>
</tr>");
print ("</table>");
//} //Closing display_form function block.
//display_form(); //This calls on the display_form function.
include ("footer.php"); //Footer file.
?>
</form>
</td>
</tr>
</table>
</body>
</html>Code: Select all
replaced byCode: Select all
[/size]