...

View Full Version : Validating a drop down menu



moverholt31
09-10-2011, 07:34 PM
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.



<?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.

Chris Hick
09-10-2011, 08:39 PM
Change this:


if(!isset($_POST['gender'])){$errors ['gender'] = "You must select a gender.";}


To this:


if(!isset($_POST['gender'] || $_POST['gender'] == "")){$errors ['gender'] = "You must select a gender.";}
// or you could do this
if(!isset($_POST['gender'] || empty($_POST['gender'])){$errors ['gender'] = "You must select a gender.";}

BluePanther
09-10-2011, 08:46 PM
First off, use CODE and PHP tags to wrap your code appropriately :) makes life easier for everyone.

What exactly is the problem with the script? I'm guessing that no errors are being logged for your gender - isset will return true every time that form is submitted. You should check for it being set, and empty. You should add an extra condition underneath that if to check for an empty value. Something like the following:


if(isset($_POST['gender'])){
if(empty($_POST['gender'])){
$errors ['gender'] = "You must select a gender.";
}
}

This way, you wont get an 'undefined index' notice for gender if the form hasn't been submitted, and you're checking for an empty value if the form has been submitted.

Also, I don't understand why you're setting a value to the select tag? The select tag holds the name, the option tag holds the value to be passed with the select tag's name. Select should have no value attribute.

moverholt31
09-12-2011, 02:09 AM
Change this:


if(!isset($_POST['gender'])){$errors ['gender'] = "You must select a gender.";}


To this:


if(!isset($_POST['gender'] || $_POST['gender'] == "")){$errors ['gender'] = "You must select a gender.";}
// or you could do this
if(!isset($_POST['gender'] || empty($_POST['gender'])){$errors ['gender'] = "You must select a gender.";}


Thank you. The first selection worked fantastically.:):thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum