john89 Posted January 10, 2011 Share Posted January 10, 2011 Hello guys I've hit a problem whicle trying to validate my form. I have 3 drop down boxes where the user chooses from three options. But I can't seem to figure out how to set the validation so the user does not select the same option in each drop down. Can anyone help me solve this please. Thank you. <p><b>Course Choice 1</b> <select name="course1"> <option value="0"></option> <option value="Business computer Systems">Business computer Systems</option> <option value="Business computer Science">Business computer Science</option> <option value="Business computer Science (Games)">Business computer Science (Games)</option> <option value="Business Information Systems">Business Information Systems</option> <option value="Digital Media Development">Digital Media Development</option> <option value="Digital Media">Digital Media</option> </select></p> <p><b>Course Choice 2</b> <select name="course2"> <option value="Leave Blank">Leave Blank</option> <option value="Business computer Systems">Business computer Systems</option> <option value="Business computer Science">Business computer Science</option> <option value="Business computer Science (Games)">Business computer Science (Games)</option> <option value="Business Information Systems">Business Information Systems</option> <option value="Digital Media Development">Digital Media Development</option> <option value="Digital Media">Digital Media</option> </select></p> <p><b>Course Choice 3</b> <select name="course3"> <option value="Leave Blank">Leave Blank</option> <option value="Business computer Systems">Business computer Systems</option> <option value="Business computer Science">Business computer Science</option> <option value="Business computer Science (Games)">Business computer Science (Games)</option> <option value="Business Information Systems">Business Information Systems</option> <option value="Digital Media Development">Digital Media Development</option> <option value="Digital Media">Digital Media</option> </select></p> <div align="centre"><input type="submit" name="submit" value="send request" /></div> //Validate course choice 1 if (!empty($_REQUEST['course1'])) { $course1 = $_REQUEST['course1']; } else { $course1 = NULL; echo '<p><font color="red">Please enter your first choice</font></p>'; } //Validate course choice 2 if (!empty($_REQUEST['course2'])) { $course2 = $_REQUEST['course2']; } else { $course2 = NULL; echo '<p><font color="red">Please enter your second choice</font></p>'; } //Validate course choice 3 if (!empty($_REQUEST['course3'])) { $course3 = $_REQUEST['course3']; } else { $course3 = NULL; echo '<p><font color="red">Please enter your third choice</font></p>'; } //If everything is ok, print the message if ($name && $email && $course1 && $course2 && $course3) { echo "<p>Thank you, <b>$name</b>, You have chosen the following courses for information:<br /><br /> <b>$course1</b><br /> <b>$course2</b><br /> <b>$course3</b></p> <p>We will reply to you at <i>$email</i>.</p>\n"; } else { // One form element was not filled out properly echo '<p><font color="red">Please go back and fill out the form again.</font></p>'; } Quote Link to comment Share on other sites More sharing options...
Rifts Posted January 10, 2011 Share Posted January 10, 2011 off the top of my head if( $value1 == $value2 || $value1 == $value3 || $value2 == $value3 ) { echo "your values are the same dickhead"; } else { echo "you rock"; } Quote Link to comment Share on other sites More sharing options...
john89 Posted January 11, 2011 Author Share Posted January 11, 2011 Yeah that code kind of works but it's a bit dodgy. I mean when I type in the details and choose the same courses I don't get the error but say I miss out 'name' or 'email', then an error appears saying "Please enter your name" AND then an error appears saying "You cannot select the same choice". Why is that happening? Quote Link to comment Share on other sites More sharing options...
john89 Posted January 11, 2011 Author Share Posted January 11, 2011 EDIT: Ok the problem is slightly different now. Whats happening is that the code works but underneath I'm getting the normal message as well saying "thank you for you interests etc" Anyway of overcoming this? Quote Link to comment Share on other sites More sharing options...
Rifts Posted January 11, 2011 Share Posted January 11, 2011 just post the code of the process form Quote Link to comment Share on other sites More sharing options...
john89 Posted January 11, 2011 Author Share Posted January 11, 2011 This is it: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title></title> </head> <body> <?php #Script 1.0 - Validating contact form //Validate the name if (!empty($_REQUEST['name'])) { $name = stripslashes($_REQUEST['name']); } else { $name = NULL; echo '<p><font color="red">Please enter your name</font></p>'; } //Validate the email if (!empty($_REQUEST['email'])) { $email = $_REQUEST['email']; } else { $email = NULL; echo '<p><font color="red">Please enter your email address</font></p>'; } //Validate course choice 1 if (!empty($_REQUEST['course1'])) { $course1 = $_REQUEST['course1']; } else { $course1 = NULL; echo '<p><font color="red">Please enter your first choice</font></p>'; } //Validate course choice 2 if (!empty($_REQUEST['course2'])) { $course2 = $_REQUEST['course2']; } else { $course2 = NULL; echo '<p><font color="red">Please enter your second choice</font></p>'; } //Validate course choice 3 if (!empty($_REQUEST['course3'])) { $course3 = $_REQUEST['course3']; } else { $course3 = NULL; echo '<p><font color="red">Please enter your third choice</font></p>'; } //Cannot choose the same option if( $course1 == $course2 || $course1 == $course3 || $course2 == $course3 ) { echo "You cannot choose the same choice."; } else { echo ""; } //If everything is ok, print the message if ($name && $email && $course1 && $course2 && $course3) { echo "<p>Thank you, <b>$name</b>, You have chosen the following courses for information:<br /><br /> <b>$course1</b><br /> <b>$course2</b><br /> <b>$course3</b></p> <p>We will reply to you at <i>$email</i>.</p>\n"; } else { // One form element was not filled out properly echo '<p><font color="red">Please go back and fill out the form again.</font></p>'; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Rifts Posted January 11, 2011 Share Posted January 11, 2011 change this //Cannot choose the same option if( $course1 == $course2 || $course1 == $course3 || $course2 == $course3 ) { echo "You cannot choose the same choice."; } else { echo ""; } //If everything is ok, print the message if ($name && $email && $course1 && $course2 && $course3) { echo "<p>Thank you, <b>$name</b>, You have chosen the following courses for information:<br /><br /> <b>$course1</b><br /> <b>$course2</b><br /> <b>$course3</b></p> <p>We will reply to you at <i>$email</i>.</p>\n"; } else { // One form element was not filled out properly echo '<p><font color="red">Please go back and fill out the form again.</font></p>'; } to //Cannot choose the same option if( $course1 == $course2 || $course1 == $course3 || $course2 == $course3 ) { echo "You cannot choose the same choice."; echo '<p><font color="red">Please go back and fill out the form again.</font></p>'; } else { //If everything is ok, print the message echo "<p>Thank you, <b>$name</b>, You have chosen the following courses for information:<br /><br /> <b>$course1</b><br /> <b>$course2</b><br /> <b>$course3</b></p> <p>We will reply to you at <i>$email</i>.</p>\n"; } Quote Link to comment Share on other sites More sharing options...
john89 Posted January 13, 2011 Author Share Posted January 13, 2011 Thnaks for your help. It works now Quote Link to comment Share on other sites More sharing options...
john89 Posted January 17, 2011 Author Share Posted January 17, 2011 Got another question for validation but this time it's for email. Any ideas how to do this. I've been playing around wiith my code using this code: $email = "some@email.com"; if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)?*(\.[a-z]{2,3})$", $email)) {echo "invalid";}[/ else {echo "valid";} But it doesn't seem to work. Any help please? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 17, 2011 Share Posted January 17, 2011 ereg functions are deprecated. If you're using php version 5.2.0 or higher, there's filter_var with the FILTER_VALIDATE_EMAIL modifier. Quote Link to comment Share on other sites More sharing options...
john89 Posted January 17, 2011 Author Share Posted January 17, 2011 Ok, thats kinda confused me, Sorry i've been trying to get my validation to work half the day and now my brain is just dead. How would I implement this into my code? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 17, 2011 Share Posted January 17, 2011 Off the top of my head . . . $email = 'good@address.com'; if( filter_var( $email, FILTER_VALIDATE_EMAIL) ) { // address validates } else { // address doesn't validate } [/code] 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.