Lisa23 Posted April 12, 2010 Share Posted April 12, 2010 depending on which radio buttom user select it changes drop list fields which forces user to select from the list but because thers actually 3 drop down as long user select from one of then pass validation if none of them fail valiadtion <?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">off</label> <input type="radio" name="campustype" value="1" onclick="toggleSelect('1');" /><label for="campustype">1</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> Link to comment https://forums.phpfreaks.com/topic/198329-how-can-i-validate-3-drop-list-as-long-user-select-from-one-of-the-list-pass-val/ Share on other sites More sharing options...
monkeytooth Posted April 13, 2010 Share Posted April 13, 2010 try using empty() or !isset() when checking your _POST/_GET data. I personally like empty(), The reason I say this is think outside of the box. Not everyone is going to use your site as its designed, eventually there may be a possibility someones gonna come along and try to mess it up for there own malicious intentions. Nip it in the bud while you can. Always always always verify your inputs, and check to make sure whats going into them is something allowed. Forms are our best friends for a user based site. But our worst enemy cause it can turn out coding against us if we don't practice safe measures. Sorry to go on a rant about injection protection and script injection but the outside the box concept is someone makes a form hosts it on there site it mimics yours goes to your site an all else, but if you aren't checking them proper, on your end, bad things can and eventually will happen. anyway the empty() function checks to see if theres just whitespace, or nothing is set, if it is set I would run it through things like preg_match, isnumeric, etc.. pending on which type of info your expecting. Link to comment https://forums.phpfreaks.com/topic/198329-how-can-i-validate-3-drop-list-as-long-user-select-from-one-of-the-list-pass-val/#findComment-1040737 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.