View Full Version : Forms: To force user choose an option in dropdown menu

07-14-2006, 02:22 PM
Hello dear coders. I bow down before you :)
(I roughly made a search in the forum, but couldn't find the answer. So if it was asked before, please help me find the thread. Im and old guy :p )

I have a form (oh how nice), which I used a generator, to create.
So, I don't know too much about html and php coding. Im a good tweaker, thats all.

I have a dropdown menu (which is a required field), and 4 options in it. But because the 1st option is default(ly appears), user is able to skip the dropdown without consciously choosing an option.

I want to force them to choose an option. Not the 1st option which appears.

I tried to put additional option, which will not exist in php process file, but I was not successfull. Here's the stripped down codes, that I have:

HTML form file:

<b>All fields marked with a * are required:</b><br>
<form enctype='multipart/form-data' action='process.php' method='post'>
<table width='50%' border=0>
<input type=text name='UserChoice'></td></tr>
<tr><td>Your Choice<font color='#ff0000'>*</font></td>
<select name='UserChoice'>
<option value='A1'>A1</option>
<option value='B2'>B2</option>
<option value='C3'>C3</option>
<option value='D4'>D4</option>
<input type='submit' value='Submit Form'>

and this is my PHP process file (process.php)

$error="The following errors occured while processing your form input.<ul>";
if($UserChoice=="" ){
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
if($errors==1) echo $error;
$message="Your Choice ".$UserChoice."
$message = stripslashes($message);
mail("my@mail.com","Form Submitted at your website",$message,"From: Generator");
$link = mysql_connect("localhost","user","password");
$query="insert into tableName
(Your Choice) values
$to_put .= $UserChoice."

<!-- Content of the Thank you page -->

<h2>Thank you!</h2>

<table width=50%>
<tr><td>Your Choice </td><td> <?php echo $UserChoice; ?> </td></tr>

You got what it is, but little bit more info:
process file is able to report, if a required field left empty. And reports it before it posts the form to MySQL database.

How to solve my problem, dear masters?
Thank you in advance.

PS. If I'm not clear enough, please let me know.

07-14-2006, 02:48 PM
Add a default option to your select tags, like so:

<select name='UserChoice'>
<option value='default' selected='selected'>--Select An Option--</option>
<option value='A1'>A1</option>
<option value='B2'>B2</option>
<option value='C3'>C3</option>
<option value='D4'>D4</option>

then in your PHP, have something like:

if ($UserChoice == "default")
die('You didn;t select an option.');

07-14-2006, 03:41 PM
"die" is a pretty nasty code :P :)

So, you think I should add this line of code in php file somewhere, or should I understand my existing process.php and adapt the thing you've wrote?

You know, I have

if($UserChoice=="" ){

I believe it'd work, but I want to keep the codes I have, and modify them. So next time I tweak something, I won't feel like an idiot :)

So, if I'd attemt to modify it with your advice, I'd do:

if($UserChoice=="default" ){
$error.="<li>You did not enter one or more of the required fields. Please go back and try again.";
would it be ok, for the rest of the function? Like listing the error message and so on?

Thanks a lot!

Edit: Better than asking... I tried it and it works. Thank you for understanding and help, again :)

07-14-2006, 04:47 PM
There are still ways to go through the code given to you, though (as in deselecting every option). I'd suggest using

if(empty($UserChoice)) {
//Errors and such