I'm having some trouble validating my gender drop down menu. I just want to make sure that something is selected.
Any help is greatly appreciated.
This is highly abbreviated. I have several other forms of input on this form, but I'm particularly having trouble with validating the drop down menus I'm using. The gender is the shortest.
Thanks again for any & all help.
Any help is greatly appreciated.
PHP Code:
<?php
$errors = array();
//Check that a gender has been selected
if(!isset($_POST['gender'])){$errors ['gender'] = "You must select a gender.";}
//If no validation errors
if(0 === count($errors)){
$gender = mysql_real_escape_string($_POST['gender']);
}
function form_row_class($name){
global $errors;
return $errors[$name] ? "form_error_row" : "";
}
function error_for($name){
global $errors;
if($errors[$name]){
return "<div style='color: red;' class='form_error'>" . $errors[$name] . "</div>";
}
}
function h($string){
return htmlspecialchars($string);
}
?>
<html>
<body>
<table>
<tr class="<?php echo form_row_class("gender") ?>">
<td class="fields" width="25%" align="right" valign="middle">
Gender:
</td>
<td width="193">
<select name="gender" id="gender" value="<?php h($_POST['gender']);?>">
<option selected = " ">Pleae Select One:</option>
<option value = "Male">Male</option value>
<option value = "Female">Female</option value>
</select>
<?php echo error_for('gender') ?>
</td>
</tr>
</table>
</body>
</html>
This is highly abbreviated. I have several other forms of input on this form, but I'm particularly having trouble with validating the drop down menus I'm using. The gender is the shortest.
Thanks again for any & all help.
Comment