Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Oklahoma City, OK
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Validating a drop down menu

    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 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(=== 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.
    Last edited by Inigoesdr; 09-12-2011 at 05:40 PM.

  • #2
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    484
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Change this:
    PHP Code:
    if(!isset($_POST['gender'])){$errors ['gender'] = "You must select a gender.";} 
    To this:
    PHP Code:
    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.";} 
    Last edited by Chris Hick; 09-10-2011 at 08:41 PM.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • Users who have thanked Chris Hick for this post:

    moverholt31 (09-12-2011)

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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:
    PHP Code:
    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.

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Location
    Oklahoma City, OK
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Worked

    Quote Originally Posted by Chris Hick View Post
    Change this:
    PHP Code:
    if(!isset($_POST['gender'])){$errors ['gender'] = "You must select a gender.";} 
    To this:
    PHP Code:
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •