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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Validating drop-down menu

    I'm building a PHP form which uses a drop-down menu. I want to validate that an option has been selected before submitting the form. My validation is always failing (I'm sure this has a simple fast solution!)


    PHP Code:
      $roundtables trim($_POST['roundtables']);
      if (
    $roundtables 'Please Select') {
          
    $error['roundtables'] = 'Please select your roundtable';
      } 

      
    $roundtableslist = array(=> 'Please Select',
                    
    =>  'Art',
                    
    =>  'Music',
                    
    =>  'Special Education'
                    
    ); 

    PHP Code:
            <label for="roundtables">Roundtables:</label> <?php if 
    (isset(
    $error['roundtables'])) { ?>
            <span class="warning"><?php echo $error['roundtables']; ?></span>
            <?php ?>
            <br />
          <select name="roundtables" id="roundtables"> 
            <?php foreach($roundtableslist as $id => $value) { ?>
                      <option value="<?php echo $value?> "
                    <?php if (isset($error) && $roundtables == $value ) { ?>
                        selected="selected" <?php   }  ?>><?php echo $value?></option>    
        <?php ?> 
           </select>
    "if ($roundtables = 'Please Select')" is always evaluating true, it seems.

    Muchas gracias in advance for your help!!

    --Diana
    Last edited by Opally; 07-30-2008 at 08:27 PM.

  • #2
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    $roundtables = 'Please Select'
    Should be:
    $roundtables == 'Please Select'

  • Users who have thanked Apothem for this post:

    Opally (07-30-2008)

  • #3
    New Coder
    Join Date
    Feb 2006
    Location
    Charlottesville, VA
    Posts
    94
    Thanks
    5
    Thanked 0 Times in 0 Posts
    heh, I kept plugging away after I posted that, and I just figured it out too. You're exactly right, it's the old double-equals-sign comparator trick.

    thanks a bunch for posting!


  •  

    Posting Permissions

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