vinsux Posted August 14, 2012 Share Posted August 14, 2012 hi again... i need a help again,.. i need an error trapping for my code.. if the selected type of account is faculty, the default course in the drop down menu must be "na" if the selected type of account is student, then it cannot accept the "na" value because he's a student... i'm tired finding a solution and researching, please help me again.. tnx in advance.. <?php mysql_connect("localhost", "root","")or die(mysql_error()); mysql_select_db("login") or die(mysql_error()); $errors=""; $message=""; if (isset($_POST['submit'])) { if (empty($errors)){ $_POST['type'] = mysql_real_escape_string($_POST['type']); $_POST['course'] = mysql_real_escape_string($_POST['course']); $check = "SELECT STATEMENT"; $result = mysql_query($check) or die(mysql_error()); $insert = "INSERT STATEMENT"; $add_member = mysql_query($insert) or die(mysql_error()); if (mysql_affected_rows()==1){ echo '<script type="text/javascript">alert(" '.$_POST['namelast'].', '.$_POST['namefirst'].' has been Registered!")</script>'; $message .= "<br><br><h1>Registered</h1> <br><br><p>Thank you, the user ".$_POST['namelast']." registered..</p> <p><a href = 'faculty.php'>[Click here]</a> to go back to home page or <a href = 'register.php'>[click here]</a> to register again<br> <br>"; } } ?> <?php if (!empty($message)){ echo "$message"; }else{ if (!empty($errors)){ echo "<br />$errors";} ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0"> <tr> <th>Type of Account:</th> <th colspan="3"> <select name= "type"> <?php $atype = array( "students" => "Student", "faculty" => "Teacher" ); foreach($atype as $tkey => $tvalue){ $selected_type = (isset($_POST['type']) && $_POST['type']==$tkey ? 'selected="selected"' : ''); echo "<option value=\"$tkey\" $selected_type>$tvalue</option>\r"; } ?> </select> </th> </tr> <tr> <th> *Course:</th> <th colspan="3"> <select name= "course"> <?php $courses = array( "na" => "---N.A.----", "bscs" => "B.S. Computer Science", "bsit" => "B.S. Information Technology", "bsba" => "B.S. Business Administration" ); foreach($courses as $key => $value){ $selected_course = (isset($_POST['course']) && $_POST['course']==$key ? 'selected="selected"' : ''); echo "<option value=\"$key\" $selected_course>$value</option>\r"; } ?> </select> </th> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 14, 2012 Share Posted August 14, 2012 Well, it all depends on what you want to do with the error, but you could do something like this: if( ($_POST['type']=='faculty' && $_POST['course'] != 'na') || ($_POST['type']!='faculty' && $_POST['course'] == 'na')) { //Invalid } or you could just populate the course list without the 'na' option. Then, put some text on the form that when selecting 'student' that a course must also be selected. Then when the form is posted if faculty is selected just ignore the course selection. Also, why are you escaping the input for query use before you have done all your validations? That will lead to problems eventually. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.