Required fields in a form?
| From: | Erica Douglass | Date: | Fri, 13 Oct 2000 23:33:45 +0000 |
| Subject: | Required fields in a form? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-20105@lists.php.net to get a copy of this message | ||
I am trying to build a form that checks if certain required fields are filled out. Not all fields are required, hence my problems.
The form calls from a table called "form_fields" that, very simplified, looks like this:
field_name | required
__________________________
fname | yes
lname | yes
wphone | no
The form is pulled from the same table. Form value names are evaluated as field_name from the database. Hence, a text field asking for the user's first name looks like this when initially called from the database: <input type="text" name="fname" size="30" value="">
The code I am using to check for required fields looks like this:
# check to make sure required fields are filled in
$query = "select field_name from form_fields where required=\"yes\"";
$result = mysql_query ($query) or die ("The server is down. Here's what MySQL said: " . mysql_error());
$required_fields = mysql_fetch_array($result);
while(list($key,$val) = each($required_fields)) {
if ($HTTP_POST_VARS[$key] == "") {
echo "$key is blank!";
}
else {
echo "$key is not blank!";
};
};
I am using the echo statements as placeholders for now.
This code, when put to use, gives back the following output (regardless of whether the form fields are actually blank or not):
0 is blank!field_name is blank!
What on earth am I doing wrong? I have a feeling that I have the right idea, but that my code is off. Please help! :)
Erica