How to validate drop list as long user select from one of the list options

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lisa007
    New Member
    • Apr 2010
    • 29

    How to validate drop list as long user select from one of the list options

    i have 3 drop down lists as long the user selects one of the option from one of the drop downs pass validation but if users don't select an option from at least one of the drop down fail validation.

    Code:
     <?php
    // Create an empty array to hold the error messages.
    $arrErrors = array();
    //Only validate if the Submit button was clicked.
    if (!empty($_POST['Submit'])) {
        // Each time there's an error, add an error message to the error array
        // using the field name as the key.
        	
    		if ($_POST['campustype']=='')
            $arrErrors['campustype'] = 'Please select category.';
    		
    		if ($_POST['howt']=='')
            $arrErrors['howt'] = 'select one field.';
    		
        if (count($arrErrors) == 0) {
            // If the error array is empty, there were no errors.
            // Insert form processing here.
        } else {
            // The error array had something in it. There was an error.
            // Start adding error text to an error string.
            $strError = '<div class="formerror"><p><img src="/images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
            // Get each error and add it to the error string
            // as a list item.
            foreach ($arrErrors as $error) {
                $strError .= "<li>$error</li>";
            }
            $strError .= '</ul></div>';
        }
    }
    ?>
    
    <style>
    .formerror {
      border: 1px solid red;
      background-color : #FFCCCC;
      width: auto;
      padding: 5px 0;
    }
    
    .errortext {
      padding-left: 80px;
      font-size:14px;
      color:red;
     
    }
    </style>
    
    <script type="text/javascript">
    function toggleSelect(id)
    {
        if (id == 'off')
        {
              document.getElementById('in-campu').style['display'] = 'none'; //ui
              document.getElementById('1').style['display'] = 'none';
              document.getElementById('off-campus').style['display'] = 'block';
        }
    
        if (id == 'in')
        {
              document.getElementById('off-campus').style['display'] = 'none';
              document.getElementById('1').style['display'] = 'none';
              document.getElementById('in-campu').style['display'] = 'block';//ui
        }
    
    if (id == '1')
        {
              document.getElementById('off-campus').style['display'] = 'none';
              document.getElementById('in-campu').style['display'] = 'none'; //ui
              document.getElementById('1').style['display'] = 'block';
        }
    }
    </script>
    
    <?php echo $strError; ?>
    <form method="post" action="<?php echo $PHP_SELF; ?>">
    <!--
    For every form field, we do the following...
    
    Check to see if there's an error message for this form field. If there is,
    add the formerror class to the surrounding paragraph block. The formerror
    class contains the highlighted box.
    
    Insert the contents of what the user submitted bak into the form field.
    
    Check again to see if this field has an error message. If it does, show
    the error icon and the error message next to the field.
    -->
    
    <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
    
        <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
    <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
    
    <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">Sports</label>
    
    <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">Art</label>
    
    </p>
    
    <p<?php if (!empty($arrErrors['howt'])) echo ' class="formerro"'; ?>>
    
        <?php if (!empty($arrErrors['howt'])) echo '<br /><span class="errortext">'.$arrErrors['howt'].'</span>'; ?><br/>
    	
    <select id="in-campu" name="howt">
     <option name="hot"  value ="">--Select  Music Type--</option>
    <option name="how" value="tuiy">Concerts</option>
    <option name="hot" value="tfyrty" >Clubs</option>
    <option name="hot" value="rtyyt">Festival</option>
    <option name="hot" value="uyity">Opera</option>
    </select>
    
        <select id="off-campus" class="item" name="hot" style="display: none;">
     <option name="dg" value=""> -- Select Sport Type -- </option>
    <option name="hot" value="dfg">Formula 1</option>
    <option name="hot" value="dfrg">Footbal</option>
    <option name="hot" value="dfgf">Basketball</option>
    <option name="how" value="rugby">Rugby</option>
    <option name="hot" value="cricket">Cricket</option>
    </select>
    
        <select id="1" class="item" name="ho" style="display: none;">
    <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
    <option name="ht" value="hjk">Comedy</option>
    <option name="ho"  value=" gfhftgh">Drama</option>
    <option name="hghjot" value="ioiui">Museus</option>
    </select>
    
    </p>
    <p>
        <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
  • rahuljaiswal1987
    New Member
    • Apr 2010
    • 23

    #2
    Its very tuff to understand why you want to VALIDATE drop down boxes as its the gun treatment for end user for not doing invalid things.

    Comment

    • NettSIte
      New Member
      • Jun 2009
      • 7

      #3
      You are not providing enough information: do you want to do a validation at the server to check that a value has been selected from at least select?

      Code:
      <?php
      if (empty($_REQUEST['select1']) && empty($_REQUEST['select2']) ) {
        echo "You have to select something somewhere!"; // do error stuff
      }
      ?>
      Last edited by Dormilich; Apr 13 '10, 08:15 PM. Reason: Please use [code] tags when posting code

      Comment

      • lisa007
        New Member
        • Apr 2010
        • 29

        #4
        nobody seems to understand my question a picture speaks a thousand words please here's the whole code can u guys run please and you notice that drop list doesnt get validate even if the user select one option reason is there's actually 3 drop list depending on which radio the user select so if user select music a music drop down list will be on dipslay all the other will be kept hideen but unfortunally to pass the validation the user has to click each radio one by one and each time slect from their respective drop down please run the whole code for better understand help please

        Code:
         <?php
        // Create an empty array to hold the error messages.
        $arrErrors = array();
        //Only validate if the Submit button was clicked.
        if (!empty($_POST['Submit'])) {
            // Each time there's an error, add an error message to the error array
            // using the field name as the key.
            if ($_POST['name']=='')
                $arrErrors['name'] = 'Please provide your name.';
            if ($_POST['email']=='')
                $arrErrors['email'] = 'A valid email address is required.';
            if ($_POST['phone']=='')
                $arrErrors['phone'] = 'Please provide your phone number.';
        		
        		if ($_POST['campustype']=='')
                $arrErrors['campustype'] = 'Please select category.';
        		
        		if ($_POST['howt']=='')
                $arrErrors['howt'] = 'select one field.';
        		
            if (count($arrErrors) == 0) {
                // If the error array is empty, there were no errors.
                // Insert form processing here.
            } else {
                // The error array had something in it. There was an error.
                // Start adding error text to an error string.
                $strError = '<div class="formerror"><p><img src="/images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
                // Get each error and add it to the error string
                // as a list item.
                foreach ($arrErrors as $error) {
                    $strError .= "<li>$error</li>";
                }
                $strError .= '</ul></div>';
            }
        }
        ?>
        
        <style>
        .formerror {
          border: 1px solid red;
          background-color : #FFCCCC;
          width: auto;
          padding: 5px 0;
        }
        
        .errortext {
          padding-left: 80px;
          font-size:14px;
          color:red;
        }
        </style>
        
        <script type="text/javascript">
        function toggleSelect(id)
        {
            if (id == 'off')
            {
                  document.getElementById('in-campu').style['display'] = 'none'; //ui
                  document.getElementById('1').style['display'] = 'none';
                  document.getElementById('off-campus').style['display'] = 'block';
            }
        
            if (id == 'in')
            {
                  document.getElementById('off-campus').style['display'] = 'none';
                  document.getElementById('1').style['display'] = 'none';
                  document.getElementById('in-campu').style['display'] = 'block';//ui
            }
        
        if (id == '1')
            {
                  document.getElementById('off-campus').style['display'] = 'none';
                  document.getElementById('in-campu').style['display'] = 'none'; //ui
                  document.getElementById('1').style['display'] = 'block';
            }
        }
        </script>
        
        <?php echo $strError; ?>
        <form method="post" action="<?php echo $PHP_SELF; ?>">
        <!--
        For every form field, we do the following...
        
        Check to see if there's an error message for this form field. If there is,
        add the formerror class to the surrounding paragraph block. The formerror
        class contains the highlighted box.
        
        Insert the contents of what the user submitted bak into the form field.
        
        Check again to see if this field has an error message. If it does, show
        the error icon and the error message next to the field.
        -->
        <p<?php if (!empty($arrErrors['name'])) echo ' class="formerro"'; ?>>
        
            <?php if (!empty($arrErrors['name'])) echo '<br /><span class="errortext">'.$arrErrors['name'].'</span>'; ?>
        <br />
            <label for="name">Name:</label>
            <input name="name" type="text" id="name" value="<?php echo $_POST['name'] ?>">
        </p>
        
        <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
        
            <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
        <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
        
        <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">Sports</label>
        
        <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">Art</label>
        
        </p>
        
        <p<?php if (!empty($arrErrors['howt'])) echo ' class="formerro"'; ?>>
        
            <?php if (!empty($arrErrors['howt'])) echo '<br /><span class="errortext">'.$arrErrors['howt'].'</span>'; ?><br/>
        	
        <select id="in-campu" name="howt">
         <option name="hot"  value ="">--Select  Music Type--</option>
        <option name="how" value="tuiy">Concerts</option>
        <option name="hot" value="tfyrty" >Clubs</option>
        <option name="hot" value="rtyyt">Festival</option>
        <option name="hot" value="uyity">Opera</option>
        </select>
        
            <select id="off-campus" class="item" name="hot" style="display: none;">
         <option name="dg" value=""> -- Select Sport Type -- </option>
        <option name="hot" value="dfg">Formula 1</option>
        <option name="hot" value="dfrg">Footbal</option>
        <option name="hot" value="dfgf">Basketball</option>
        <option name="how" value="rugby">Rugby</option>
        <option name="hot" value="cricket">Cricket</option>
        </select>
        
            <select id="1" class="item" name="ho" style="display: none;">
        <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
        <option name="ht" value="hjk">Comedy</option>
        <option name="ho"  value=" gfhftgh">Drama</option>
        <option name="hghjot" value="ioiui">Museus</option>
        </select>
        
        </p>
        
        <p<?php if (!empty($arrErrors['email'])) echo ' class="formerro"'; ?>>
        
            <?php if (!empty($arrErrors['email'])) echo '<br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?>
        <br />
            <label for="email">Email:</label>
            <input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>">
        </p>
        
        <p<?php if (!empty($arrErrors['phone'])) echo ' class="formerro"'; ?>>
        
            <?php if (!empty($arrErrors['phone'])) echo '<br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?>
        <br />
            <label for="phone">Phone:</label>
            <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>">
        </p>
        
        <p>
            <input type="submit" name="Submit" value="Submit">
        </p>
        </form>

        Comment

        • NettSIte
          New Member
          • Jun 2009
          • 7

          #5
          Hi Lisa,

          Code:
          if ($_POST['campustype']=='') { 
                  	$arrErrors['campustype'] = 'Please select category.'; 
                  }
                  else {
                  	switch ($_POST['campustype']) {
                  		case "in":
                  			if ($_POST['howt']=='') $arrErrors['howt'] = 'select one field.';
                  			break;
                  		case "out":
                  			if ($_POST['hot']=='') $arrErrors['hot'] = 'select one field.';
                  			break;
                  		case "1":
                  			if ($_POST['ho']=='') $arrErrors['ho'] = 'select one field.';
                  			break;
                  	}
          Should work.
          Last edited by Dormilich; Apr 13 '10, 08:18 PM. Reason: Please use [code] tags when posting code

          Comment

          • lisa007
            New Member
            • Apr 2010
            • 29

            #6
            Originally posted by NettSIte
            Hi Lisa,

            Code:
            if ($_POST['campustype']=='') { 
                    	$arrErrors['campustype'] = 'Please select category.'; 
                    }
                    else {
                    	switch ($_POST['campustype']) {
                    		case "in":
                    			if ($_POST['howt']=='') $arrErrors['howt'] = 'select one field.';
                    			break;
                    		case "out":
                    			if ($_POST['hot']=='') $arrErrors['hot'] = 'select one field.';
                    			break;
                    		case "1":
                    			if ($_POST['ho']=='') $arrErrors['ho'] = 'select one field.';
                    			break;
                    	}
            Should work.
            almost did work but one problem

            <?php if (!empty($arrErr ors['campustype'])) echo '<br /><span class="errortex t">'.$arrErr ors['campustype'].'</span>'; ?>

            which this call the error campustype so as long the user select one radio it pass the validation without the user even have to select from the drop down list how can i get that line to display error depending on the 3 drop downs not the radio i've added 2 comments named (newbie) thats one with your new code and the other where the problems is on line 151 and 153 cz that call for the error which is campustype i tried.. call for error howt or how or ho.. still didnt work

            Code:
            <?php
            
            // Create an empty array to hold the error messages.
            $arrErrors = array();
            //Only validate if the Submit button was clicked.
            if (!empty($_POST['Submit'])) {
                // Each time there's an error, add an error message to the error array
                // using the field name as the key.
                if ($_POST['name']=='')
                    $arrErrors['name'] = 'Please provide your name.';
                if ($_POST['email']=='')
                    $arrErrors['email'] = 'A valid email address is required.';
                if ($_POST['phone']=='')
                    $arrErrors['phone'] = 'Please provide your phone number.';
            		
            		// Newbie your code starts here
            		if ($_POST['campustype']=='')
                    $arrErrors['campustype'] = 'Please select category.';
            		
                    }
                    else {
                    	switch ($_POST['campustype']) {
                    		case "in":
                    			if ($_POST['howt']=='') $arrErrors['howt'] = 'select one field.';
                    			break;
                    		case "in":
                    			if ($_POST['hot']=='') $arrErrors['hot'] = 'select one field.';
                    			break;
                    		case "1":
                    			if ($_POST['ho']=='') $arrErrors['ho'] = 'select one field.';
                    			break;
                    	}
             // Newbie your  code ends here
            		
                if (count($arrErrors) == 0) {
                    // If the error array is empty, there were no errors.
                    // Insert form processing here.
                } else {
                    // The error array had something in it. There was an error.
                    // Start adding error text to an error string.
                    $strError = '<div class="formerror"><p><img src="/images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p><ul>';
                    // Get each error and add it to the error string
                    // as a list item.
                    foreach ($arrErrors as $error) {
                        $strError .= "<li>$error</li>";
                    }
                    $strError .= '</ul></div>';
                }
            }
            ?>
            
            <style>
            .formerror {
              border: 1px solid red;
              background-color : #FFCCCC;
              width: auto;
              padding: 5px 0;
            }
            
            .errortext {
              padding-left: 80px;
              font-size:14px;
              color:red;
            }
            </style>
            
            <script type="text/javascript">
            function toggleSelect(id)
            {
                if (id == 'off')
                {
                      document.getElementById('in-campu').style['display'] = 'none'; //ui
                      document.getElementById('1').style['display'] = 'none';
                      document.getElementById('off-campus').style['display'] = 'block';
                }
            
                if (id == 'in')
                {
                      document.getElementById('off-campus').style['display'] = 'none';
                      document.getElementById('1').style['display'] = 'none';
                      document.getElementById('in-campu').style['display'] = 'block';//ui
                }
            
            if (id == '1')
                {
                      document.getElementById('off-campus').style['display'] = 'none';
                      document.getElementById('in-campu').style['display'] = 'none'; //ui
                      document.getElementById('1').style['display'] = 'block';
                }
            }
            </script>
            
            <?php echo $strError; ?>
            <form method="post" action="<?php echo $PHP_SELF; ?>">
            <!--
            For every form field, we do the following...
            
            Check to see if there's an error message for this form field. If there is,
            add the formerror class to the surrounding paragraph block. The formerror
            class contains the highlighted box.
            
            Insert the contents of what the user submitted bak into the form field.
            
            Check again to see if this field has an error message. If it does, show
            the error icon and the error message next to the field.
            -->
            <p<?php if (!empty($arrErrors['name'])) echo ' class="formerro"'; ?>>
            
                <?php if (!empty($arrErrors['name'])) echo '<br /><span class="errortext">'.$arrErrors['name'].'</span>'; ?>
            <br />
                <label for="name">Name:</label>
                <input name="name" type="text" id="name" value="<?php echo $_POST['name'] ?>">
            </p>
            
            <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
            
                <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
            <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
            
            <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">off</label>
            
            <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">1</label>
            
            </p>
            
            <?php //  Newbie  this calls only the campus error i want to call the how,hot, and ho if one of them not selected ?>
            
            <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
            
                <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/>
            	
            <select id="in-campu" name="howt">
             <option   value ="">--Select  Music Type--</option>
            <option  value="tuiy">Concerts</option>
            <option  value="tfyrty" >Clubs</option>
            <option  value="rtyyt">Festival</option>
            <option  value="uyity">Opera</option>
            </select>
            
                <select id="off-campus" class="item" name="hot" style="display: none;">
             <option name="" value=""> -- Select Sport Type -- </option>
            <option  value="dfg">Formula 1</option>
            <option  value="dfrg">Footbal</option>
            <option  value="dfgf">Basketball</option>
            <option  value="rugby">Rugby</option>
            <option  value="cricket">Cricket</option>
            </select>
            
                <select id="1" class="item" name="ho" style="display: none;">
            <option name="" value=""> -- Select Art & Theatre Type -- </option>
            <option  value="hjk">Comedy</option>
            <option   value=" gfhftgh">Drama</option>
            <option  value="ioiui">Museus</option>
            </select>
            </p>
            
            <p<?php if (!empty($arrErrors['email'])) echo ' class="formerro"'; ?>>
            
                <?php if (!empty($arrErrors['email'])) echo '<br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?>
            <br />
                <label for="email">Email:</label>
                <input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>">
            </p>
            
            <p<?php if (!empty($arrErrors['phone'])) echo ' class="formerro"'; ?>>
            
                <?php if (!empty($arrErrors['phone'])) echo '<br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?>
            <br />
                <label for="phone">Phone:</label>
                <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>">
            </p>
            
            <p>
                <input type="submit" name="Submit" value="Submit">
            </p>
            </form>

            Comment

            Working...