mikebyrne Posted March 23, 2008 Share Posted March 23, 2008 Im trying to validate a dropdown list whereby if the user doesnt select an option an error will appear My code is: if($_POST["action"] == "Add"){ $valid=1; if ($_POST['Type']=="------") { echo 'Please enter the product type<br>'; $valid=0; $style_name = "background-color:#FF5959"; $error_type = "Please enter the product type<br>"; } <select name="Type" class="000"> <option value="">------</option> <option value="Game" >GAME</option> <option value="DVD" >DVD</option> <option value="CD" >CD</option> </select> <td width="269"><font color="#FF0000" style="font-size: 8pt"><?php echo $error_type; ?></font></td> At present if I leave it at ----- I dont get a report Quote Link to comment Share on other sites More sharing options...
alin19 Posted March 23, 2008 Share Posted March 23, 2008 i'm not sure that it will work, but you can try this: <select name="Type" class="000"> <option value="------">------</option> <option value="Game" >GAME</option> <option value="DVD" >DVD</option> <option value="CD" >CD</option> </select> Quote Link to comment Share on other sites More sharing options...
Bladescope Posted March 23, 2008 Share Posted March 23, 2008 The above poster's code will work. In this example when using the drop down box: <select name="Type" class="000"> <option value="">------</option> returns $_POST['Type'] = "" <option value="Game" >GAME</option> returns $_POST['Type'] = "Game" <option value="DVD" >DVD</option> returns $_POST['Type'] = "DVD" <option value="CD" >CD</option> returns $_POST['Type'] = "CD" </select> So you see, it assigns it to the value of the option, not the text inbetween the tags. Changing the value to "------" isn't such a good idea, as it could cause problems later on, but you can change the whole 'if' argument to: if ($_POST['Type'] == "" || !isset($_POST['Type'])) or just one of the two there. Quote Link to comment Share on other sites More sharing options...
mikebyrne Posted March 23, 2008 Author Share Posted March 23, 2008 Thats fixed it. Thanks for your help 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.