Jump to content

Validating Drop down box (user cannot select the same option)


john89

Recommended Posts

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>';
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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";

} 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.