Hi i’m trying to create a form wher the user must at least select one option from the drop lists as long select one drop list pass the validation if none are selected display error message, i create an error array called music
if ($_POST[‘music’]==’ ')
$arrErrors[‘music’] = ‘select one field.’;
which means if drop down list empty display error message i want the same for the other 2 drop down list but then if user select from one drop down list pass validation
then just above the drop list is the error array been echo because the array been called music as one of the drop down list named music if you select from the music list it validates… but i want the same to happen to the others bear in mind user can only select one drop list.
here the full 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['music']=='')
$arrErrors['music'] = '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;
}
//radio java script javascript
</style>
<?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>
<?php echo"user must pick at least one form"; ?>
<p<?php if (!empty($arrErrors['howt'])) echo ' class="formerro"'; ?>>
<?php echo '<br /><span class="errortext">'.$arrErrors['music'].'</span>'; ?><br/>
<select id="off-campus" name="sport" class="item" >
<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="in-campu" name="music">
<option name="" 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="1" name="art" class="item" >
<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>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
each list depend on the category you select for example if you select SPORTS subcategory football cricket and so on if you select MUSIC subcategory concerts clubs and so on instead of making one long list with everything mixed so taking into account that he can only select one category and one subcategory as long he picks one category and subcategoty pass validation if none then fail validation
I think the more fundamental question is your application design. Why throw the error here at all, if there is an acceptable alternative? It’s harder to deal with programming errors (as you’re seeing, they’re simply application logic, and in this case it seems like needlessly added complexity), they’re a minor user interface hit (they make your user stop whatever they want to do and deal with something they don’t care about, at best errors are always a distraction). So I try to avoid them whenever possible. At the very worst, that means I have fewer of the buggers to remember and deal with when I’m maintaining the application; errors are by definition a point in your application where things can go wrong.
My recommendation would be to think about the many alternatives in your error handling toolbox besides throwing error messages. When you’re interacting with a textbox, sure, you really have no option, you need to ask your user to put something into a required field if they don’t. With a drop down lists however, the much more elegant method is to select default settings for the drop downs. I would choose a default value set for the parent drop down (whatever I thought was most likely for the user to choose, you can revisit this as your application is in use and you actually find out what options people are selecting). This will bring up the appropriate set of data for the child drop down, which should also have a default setting, in fact all the various sets of data that may populate the child drop down should have a default set. This is much simpler than programming exceptions or communicating messages, and it also guarantees that there is always a valid input for your PHP code when it processes.
The most important concept that this brings up is simply that error messages and exceptions are not the alpha and omega of error handling… they are not even the preferred method of error handling. The best way to handle errors is to design the application in such a way that such an error can’t happen. Next best, is probably programming application logic that deals with an error internally where no one ever sees it. Then displaying error messages. Then throwing exceptions, and worst case is not handling an error at all. Note that these techniques are not mutually exclusive, just remember that exceptions are horribly expensive.