Web Analytics Made Easy -
StatCounter Validating a drop down menu - CodingForum

Announcement

Collapse
No announcement yet.

Validating a drop down menu

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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; Sep 12, 2011, 12:40 PM.

  • #2
    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; Sep 10, 2011, 03: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. ^_^

    Comment


    • #3
      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.
      Useful function to retrieve difference in times
      The best PHP resource
      A good PHP FAQ
      PLEASE remember to wrap your code in [PHP] tags.
      PHP Code:
      // Replace this
      if(isset($_POST['submitButton']))
      // With this
      if(!empty($_POST))
      // Then check for values/forms. Some IE versions don't send the submit button 
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

      Comment


      • #4
        Worked

        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.

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎