By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,474 Members | 1,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,474 IT Pros & Developers. It's quick & easy.

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

P: 29
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.

Expand|Select|Wrap|Line Numbers
  1.  <?php
  2. // Create an empty array to hold the error messages.
  3. $arrErrors = array();
  4. //Only validate if the Submit button was clicked.
  5. if (!empty($_POST['Submit'])) {
  6.     // Each time there's an error, add an error message to the error array
  7.     // using the field name as the key.
  8.  
  9.         if ($_POST['campustype']=='')
  10.         $arrErrors['campustype'] = 'Please select category.';
  11.  
  12.         if ($_POST['howt']=='')
  13.         $arrErrors['howt'] = 'select one field.';
  14.  
  15.     if (count($arrErrors) == 0) {
  16.         // If the error array is empty, there were no errors.
  17.         // Insert form processing here.
  18.     } else {
  19.         // The error array had something in it. There was an error.
  20.         // Start adding error text to an error string.
  21.         $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>';
  22.         // Get each error and add it to the error string
  23.         // as a list item.
  24.         foreach ($arrErrors as $error) {
  25.             $strError .= "<li>$error</li>";
  26.         }
  27.         $strError .= '</ul></div>';
  28.     }
  29. }
  30. ?>
  31.  
  32. <style>
  33. .formerror {
  34.   border: 1px solid red;
  35.   background-color : #FFCCCC;
  36.   width: auto;
  37.   padding: 5px 0;
  38. }
  39.  
  40. .errortext {
  41.   padding-left: 80px;
  42.   font-size:14px;
  43.   color:red;
  44.  
  45. }
  46. </style>
  47.  
  48. <script type="text/javascript">
  49. function toggleSelect(id)
  50. {
  51.     if (id == 'off')
  52.     {
  53.           document.getElementById('in-campu').style['display'] = 'none'; //ui
  54.           document.getElementById('1').style['display'] = 'none';
  55.           document.getElementById('off-campus').style['display'] = 'block';
  56.     }
  57.  
  58.     if (id == 'in')
  59.     {
  60.           document.getElementById('off-campus').style['display'] = 'none';
  61.           document.getElementById('1').style['display'] = 'none';
  62.           document.getElementById('in-campu').style['display'] = 'block';//ui
  63.     }
  64.  
  65. if (id == '1')
  66.     {
  67.           document.getElementById('off-campus').style['display'] = 'none';
  68.           document.getElementById('in-campu').style['display'] = 'none'; //ui
  69.           document.getElementById('1').style['display'] = 'block';
  70.     }
  71. }
  72. </script>
  73.  
  74. <?php echo $strError; ?>
  75. <form method="post" action="<?php echo $PHP_SELF; ?>">
  76. <!--
  77. For every form field, we do the following...
  78.  
  79. Check to see if there's an error message for this form field. If there is,
  80. add the formerror class to the surrounding paragraph block. The formerror
  81. class contains the highlighted box.
  82.  
  83. Insert the contents of what the user submitted bak into the form field.
  84.  
  85. Check again to see if this field has an error message. If it does, show
  86. the error icon and the error message next to the field.
  87. -->
  88.  
  89. <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
  90.  
  91.     <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
  92. <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
  93.  
  94. <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">Sports</label>
  95.  
  96. <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">Art</label>
  97.  
  98. </p>
  99.  
  100. <p<?php if (!empty($arrErrors['howt'])) echo ' class="formerro"'; ?>>
  101.  
  102.     <?php if (!empty($arrErrors['howt'])) echo '<br /><span class="errortext">'.$arrErrors['howt'].'</span>'; ?><br/>
  103.  
  104. <select id="in-campu" name="howt">
  105.  <option name="hot"  value ="">--Select  Music Type--</option>
  106. <option name="how" value="tuiy">Concerts</option>
  107. <option name="hot" value="tfyrty" >Clubs</option>
  108. <option name="hot" value="rtyyt">Festival</option>
  109. <option name="hot" value="uyity">Opera</option>
  110. </select>
  111.  
  112.     <select id="off-campus" class="item" name="hot" style="display: none;">
  113.  <option name="dg" value=""> -- Select Sport Type -- </option>
  114. <option name="hot" value="dfg">Formula 1</option>
  115. <option name="hot" value="dfrg">Footbal</option>
  116. <option name="hot" value="dfgf">Basketball</option>
  117. <option name="how" value="rugby">Rugby</option>
  118. <option name="hot" value="cricket">Cricket</option>
  119. </select>
  120.  
  121.     <select id="1" class="item" name="ho" style="display: none;">
  122. <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
  123. <option name="ht" value="hjk">Comedy</option>
  124. <option name="ho"  value=" gfhftgh">Drama</option>
  125. <option name="hghjot" value="ioiui">Museus</option>
  126. </select>
  127.  
  128. </p>
  129. <p>
  130.     <input type="submit" name="Submit" value="Submit">
  131. </p>
  132. </form>   
Apr 13 '10 #1
Share this Question
Share on Google+
5 Replies


rahuljaiswal1987
P: 23
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.
Apr 13 '10 #2

NettSIte
P: 7
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?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (empty($_REQUEST['select1']) && empty($_REQUEST['select2']) ) {
  3.   echo "You have to select something somewhere!"; // do error stuff
  4. }
  5. ?>
Apr 13 '10 #3

P: 29
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

Expand|Select|Wrap|Line Numbers
  1.  <?php
  2. // Create an empty array to hold the error messages.
  3. $arrErrors = array();
  4. //Only validate if the Submit button was clicked.
  5. if (!empty($_POST['Submit'])) {
  6.     // Each time there's an error, add an error message to the error array
  7.     // using the field name as the key.
  8.     if ($_POST['name']=='')
  9.         $arrErrors['name'] = 'Please provide your name.';
  10.     if ($_POST['email']=='')
  11.         $arrErrors['email'] = 'A valid email address is required.';
  12.     if ($_POST['phone']=='')
  13.         $arrErrors['phone'] = 'Please provide your phone number.';
  14.  
  15.         if ($_POST['campustype']=='')
  16.         $arrErrors['campustype'] = 'Please select category.';
  17.  
  18.         if ($_POST['howt']=='')
  19.         $arrErrors['howt'] = 'select one field.';
  20.  
  21.     if (count($arrErrors) == 0) {
  22.         // If the error array is empty, there were no errors.
  23.         // Insert form processing here.
  24.     } else {
  25.         // The error array had something in it. There was an error.
  26.         // Start adding error text to an error string.
  27.         $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>';
  28.         // Get each error and add it to the error string
  29.         // as a list item.
  30.         foreach ($arrErrors as $error) {
  31.             $strError .= "<li>$error</li>";
  32.         }
  33.         $strError .= '</ul></div>';
  34.     }
  35. }
  36. ?>
  37.  
  38. <style>
  39. .formerror {
  40.   border: 1px solid red;
  41.   background-color : #FFCCCC;
  42.   width: auto;
  43.   padding: 5px 0;
  44. }
  45.  
  46. .errortext {
  47.   padding-left: 80px;
  48.   font-size:14px;
  49.   color:red;
  50. }
  51. </style>
  52.  
  53. <script type="text/javascript">
  54. function toggleSelect(id)
  55. {
  56.     if (id == 'off')
  57.     {
  58.           document.getElementById('in-campu').style['display'] = 'none'; //ui
  59.           document.getElementById('1').style['display'] = 'none';
  60.           document.getElementById('off-campus').style['display'] = 'block';
  61.     }
  62.  
  63.     if (id == 'in')
  64.     {
  65.           document.getElementById('off-campus').style['display'] = 'none';
  66.           document.getElementById('1').style['display'] = 'none';
  67.           document.getElementById('in-campu').style['display'] = 'block';//ui
  68.     }
  69.  
  70. if (id == '1')
  71.     {
  72.           document.getElementById('off-campus').style['display'] = 'none';
  73.           document.getElementById('in-campu').style['display'] = 'none'; //ui
  74.           document.getElementById('1').style['display'] = 'block';
  75.     }
  76. }
  77. </script>
  78.  
  79. <?php echo $strError; ?>
  80. <form method="post" action="<?php echo $PHP_SELF; ?>">
  81. <!--
  82. For every form field, we do the following...
  83.  
  84. Check to see if there's an error message for this form field. If there is,
  85. add the formerror class to the surrounding paragraph block. The formerror
  86. class contains the highlighted box.
  87.  
  88. Insert the contents of what the user submitted bak into the form field.
  89.  
  90. Check again to see if this field has an error message. If it does, show
  91. the error icon and the error message next to the field.
  92. -->
  93. <p<?php if (!empty($arrErrors['name'])) echo ' class="formerro"'; ?>>
  94.  
  95.     <?php if (!empty($arrErrors['name'])) echo '<br /><span class="errortext">'.$arrErrors['name'].'</span>'; ?>
  96. <br />
  97.     <label for="name">Name:</label>
  98.     <input name="name" type="text" id="name" value="<?php echo $_POST['name'] ?>">
  99. </p>
  100.  
  101. <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
  102.  
  103.     <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
  104. <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
  105.  
  106. <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">Sports</label>
  107.  
  108. <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">Art</label>
  109.  
  110. </p>
  111.  
  112. <p<?php if (!empty($arrErrors['howt'])) echo ' class="formerro"'; ?>>
  113.  
  114.     <?php if (!empty($arrErrors['howt'])) echo '<br /><span class="errortext">'.$arrErrors['howt'].'</span>'; ?><br/>
  115.  
  116. <select id="in-campu" name="howt">
  117.  <option name="hot"  value ="">--Select  Music Type--</option>
  118. <option name="how" value="tuiy">Concerts</option>
  119. <option name="hot" value="tfyrty" >Clubs</option>
  120. <option name="hot" value="rtyyt">Festival</option>
  121. <option name="hot" value="uyity">Opera</option>
  122. </select>
  123.  
  124.     <select id="off-campus" class="item" name="hot" style="display: none;">
  125.  <option name="dg" value=""> -- Select Sport Type -- </option>
  126. <option name="hot" value="dfg">Formula 1</option>
  127. <option name="hot" value="dfrg">Footbal</option>
  128. <option name="hot" value="dfgf">Basketball</option>
  129. <option name="how" value="rugby">Rugby</option>
  130. <option name="hot" value="cricket">Cricket</option>
  131. </select>
  132.  
  133.     <select id="1" class="item" name="ho" style="display: none;">
  134. <option name="hot" value=""> -- Select Art & Theatre Type -- </option>
  135. <option name="ht" value="hjk">Comedy</option>
  136. <option name="ho"  value=" gfhftgh">Drama</option>
  137. <option name="hghjot" value="ioiui">Museus</option>
  138. </select>
  139.  
  140. </p>
  141.  
  142. <p<?php if (!empty($arrErrors['email'])) echo ' class="formerro"'; ?>>
  143.  
  144.     <?php if (!empty($arrErrors['email'])) echo '<br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?>
  145. <br />
  146.     <label for="email">Email:</label>
  147.     <input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>">
  148. </p>
  149.  
  150. <p<?php if (!empty($arrErrors['phone'])) echo ' class="formerro"'; ?>>
  151.  
  152.     <?php if (!empty($arrErrors['phone'])) echo '<br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?>
  153. <br />
  154.     <label for="phone">Phone:</label>
  155.     <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>">
  156. </p>
  157.  
  158. <p>
  159.     <input type="submit" name="Submit" value="Submit">
  160. </p>
  161. </form>   
Apr 13 '10 #4

NettSIte
P: 7
Hi Lisa,

Expand|Select|Wrap|Line Numbers
  1. if ($_POST['campustype']=='') { 
  2.             $arrErrors['campustype'] = 'Please select category.'; 
  3.         }
  4.         else {
  5.             switch ($_POST['campustype']) {
  6.                 case "in":
  7.                     if ($_POST['howt']=='') $arrErrors['howt'] = 'select one field.';
  8.                     break;
  9.                 case "out":
  10.                     if ($_POST['hot']=='') $arrErrors['hot'] = 'select one field.';
  11.                     break;
  12.                 case "1":
  13.                     if ($_POST['ho']=='') $arrErrors['ho'] = 'select one field.';
  14.                     break;
  15.             }
Should work.
Apr 13 '10 #5

P: 29
@NettSIte
almost did work but one problem

<?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // Create an empty array to hold the error messages.
  4. $arrErrors = array();
  5. //Only validate if the Submit button was clicked.
  6. if (!empty($_POST['Submit'])) {
  7.     // Each time there's an error, add an error message to the error array
  8.     // using the field name as the key.
  9.     if ($_POST['name']=='')
  10.         $arrErrors['name'] = 'Please provide your name.';
  11.     if ($_POST['email']=='')
  12.         $arrErrors['email'] = 'A valid email address is required.';
  13.     if ($_POST['phone']=='')
  14.         $arrErrors['phone'] = 'Please provide your phone number.';
  15.  
  16.         // Newbie your code starts here
  17.         if ($_POST['campustype']=='')
  18.         $arrErrors['campustype'] = 'Please select category.';
  19.  
  20.         }
  21.         else {
  22.             switch ($_POST['campustype']) {
  23.                 case "in":
  24.                     if ($_POST['howt']=='') $arrErrors['howt'] = 'select one field.';
  25.                     break;
  26.                 case "in":
  27.                     if ($_POST['hot']=='') $arrErrors['hot'] = 'select one field.';
  28.                     break;
  29.                 case "1":
  30.                     if ($_POST['ho']=='') $arrErrors['ho'] = 'select one field.';
  31.                     break;
  32.             }
  33.  // Newbie your  code ends here
  34.  
  35.     if (count($arrErrors) == 0) {
  36.         // If the error array is empty, there were no errors.
  37.         // Insert form processing here.
  38.     } else {
  39.         // The error array had something in it. There was an error.
  40.         // Start adding error text to an error string.
  41.         $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>';
  42.         // Get each error and add it to the error string
  43.         // as a list item.
  44.         foreach ($arrErrors as $error) {
  45.             $strError .= "<li>$error</li>";
  46.         }
  47.         $strError .= '</ul></div>';
  48.     }
  49. }
  50. ?>
  51.  
  52. <style>
  53. .formerror {
  54.   border: 1px solid red;
  55.   background-color : #FFCCCC;
  56.   width: auto;
  57.   padding: 5px 0;
  58. }
  59.  
  60. .errortext {
  61.   padding-left: 80px;
  62.   font-size:14px;
  63.   color:red;
  64. }
  65. </style>
  66.  
  67. <script type="text/javascript">
  68. function toggleSelect(id)
  69. {
  70.     if (id == 'off')
  71.     {
  72.           document.getElementById('in-campu').style['display'] = 'none'; //ui
  73.           document.getElementById('1').style['display'] = 'none';
  74.           document.getElementById('off-campus').style['display'] = 'block';
  75.     }
  76.  
  77.     if (id == 'in')
  78.     {
  79.           document.getElementById('off-campus').style['display'] = 'none';
  80.           document.getElementById('1').style['display'] = 'none';
  81.           document.getElementById('in-campu').style['display'] = 'block';//ui
  82.     }
  83.  
  84. if (id == '1')
  85.     {
  86.           document.getElementById('off-campus').style['display'] = 'none';
  87.           document.getElementById('in-campu').style['display'] = 'none'; //ui
  88.           document.getElementById('1').style['display'] = 'block';
  89.     }
  90. }
  91. </script>
  92.  
  93. <?php echo $strError; ?>
  94. <form method="post" action="<?php echo $PHP_SELF; ?>">
  95. <!--
  96. For every form field, we do the following...
  97.  
  98. Check to see if there's an error message for this form field. If there is,
  99. add the formerror class to the surrounding paragraph block. The formerror
  100. class contains the highlighted box.
  101.  
  102. Insert the contents of what the user submitted bak into the form field.
  103.  
  104. Check again to see if this field has an error message. If it does, show
  105. the error icon and the error message next to the field.
  106. -->
  107. <p<?php if (!empty($arrErrors['name'])) echo ' class="formerro"'; ?>>
  108.  
  109.     <?php if (!empty($arrErrors['name'])) echo '<br /><span class="errortext">'.$arrErrors['name'].'</span>'; ?>
  110. <br />
  111.     <label for="name">Name:</label>
  112.     <input name="name" type="text" id="name" value="<?php echo $_POST['name'] ?>">
  113. </p>
  114.  
  115. <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
  116.  
  117.     <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/><label for="incampus">Select Category</label>
  118. <input type="radio" name="campustype" value="in" onclick="toggleSelect('in');" /><label for="campustype">Music</label>
  119.  
  120. <input type="radio" name="campustype" value="off'" onclick="toggleSelect('off');" /><label for="campustype">off</label>
  121.  
  122. <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">1</label>
  123.  
  124. </p>
  125.  
  126. <?php //  Newbie  this calls only the campus error i want to call the how,hot, and ho if one of them not selected ?>
  127.  
  128. <p<?php if (!empty($arrErrors['campustype'])) echo ' class="formerro"'; ?>>
  129.  
  130.     <?php if (!empty($arrErrors['campustype'])) echo '<br /><span class="errortext">'.$arrErrors['campustype'].'</span>'; ?><br/>
  131.  
  132. <select id="in-campu" name="howt">
  133.  <option   value ="">--Select  Music Type--</option>
  134. <option  value="tuiy">Concerts</option>
  135. <option  value="tfyrty" >Clubs</option>
  136. <option  value="rtyyt">Festival</option>
  137. <option  value="uyity">Opera</option>
  138. </select>
  139.  
  140.     <select id="off-campus" class="item" name="hot" style="display: none;">
  141.  <option name="" value=""> -- Select Sport Type -- </option>
  142. <option  value="dfg">Formula 1</option>
  143. <option  value="dfrg">Footbal</option>
  144. <option  value="dfgf">Basketball</option>
  145. <option  value="rugby">Rugby</option>
  146. <option  value="cricket">Cricket</option>
  147. </select>
  148.  
  149.     <select id="1" class="item" name="ho" style="display: none;">
  150. <option name="" value=""> -- Select Art & Theatre Type -- </option>
  151. <option  value="hjk">Comedy</option>
  152. <option   value=" gfhftgh">Drama</option>
  153. <option  value="ioiui">Museus</option>
  154. </select>
  155. </p>
  156.  
  157. <p<?php if (!empty($arrErrors['email'])) echo ' class="formerro"'; ?>>
  158.  
  159.     <?php if (!empty($arrErrors['email'])) echo '<br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?>
  160. <br />
  161.     <label for="email">Email:</label>
  162.     <input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>">
  163. </p>
  164.  
  165. <p<?php if (!empty($arrErrors['phone'])) echo ' class="formerro"'; ?>>
  166.  
  167.     <?php if (!empty($arrErrors['phone'])) echo '<br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?>
  168. <br />
  169.     <label for="phone">Phone:</label>
  170.     <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>">
  171. </p>
  172.  
  173. <p>
  174.     <input type="submit" name="Submit" value="Submit">
  175. </p>
  176. </form> 
Apr 13 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.