bal bal Posted March 26, 2009 Share Posted March 26, 2009 How can I get the value from the select box, where the value is (car, bus, motorbike), should select anyone of them from the list. if not selected it should give an warning... "select the right vehical" please help me..... thanks Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/ Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 What do you mean? If you have 3 things in the list, only those 3 things are available to be selected... Just make the first one selected by default. Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794066 Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 You should have a form tag that covers that select tag with options. Then have a submit button. Given that you used POST, you can grab it using $_POST['name_of_the_select_tag']. It should throw whatever selected value. If what you are getting at is select something that is non existing... then add another select value that serves as default and check that if it has not selected any other. If you are a beginner, here are some tutorials you can check. http://www.w3schools.com/PHP/DEfaULT.asP http://www.php.net/tut.php http://www.tizag.com/phpT/ Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794071 Share on other sites More sharing options...
bal bal Posted March 26, 2009 Author Share Posted March 26, 2009 What do you mean? If you have 3 things in the list, only those 3 things are available to be selected... Just make the first one selected by default. these are in menu bar, when I click on the menu, these 3options will come but I've to select only 1. so how can I validate it if I don't select any or if I select any one of them... Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794072 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 You can check with isset. Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794074 Share on other sites More sharing options...
unska Posted March 26, 2009 Share Posted March 26, 2009 It's actually quite basic PHP, here's the solution: <?php if ($_POST) { echo $_POST['list']; } ?> <html> <head> </head> <body> <form action="thisfile.php" method="post"> <select name="list"> <option value="car">car</option> <option value="bus">bus</option> <option value="motorbike">motorbike</option> </select> <br> <input type="submit" name="submit" value="Submit!"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794075 Share on other sites More sharing options...
bal bal Posted March 26, 2009 Author Share Posted March 26, 2009 It's actually quite basic PHP, here's the solution: <?php if ($_POST) { echo $_POST['list']; } ?> <html> <head> </head> <body> <form action="thisfile.php" method="post"> <select name="list"> <option value="car">car</option> <option value="bus">bus</option> <option value="motorbike">motorbike</option> </select> <br> <input type="submit" name="submit" value="Submit!"> </form> </body> </html> I tried with this code.... <?php if (trim($_POST['list'])<="" { $error = "Please pick from the list"; else echo "You selected ". " " . $_POST['list'] . ".<br />"; //echo $_POST['list']; } ?> but it shows me nothing. did I make any mistake? Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794102 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Yeah, you have a lot of syntactical errors, and since you don't have them turned on it just shows a blank page. If you put this code at the top of your page it will display the errors that you have: ini_set ("display_errors", "1"); error_reporting(E_ALL); Like unska said, here is the solution, they HAVE TO choose an option from the drop down: if(isset($_POST['submit'])) { echo $_POST['list']; } ?> </pre> <form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post"> car bus motorbike </form> <b Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794106 Share on other sites More sharing options...
bal bal Posted March 26, 2009 Author Share Posted March 26, 2009 Yeah, you have a lot of syntactical errors, and since you don't have them turned on it just shows a blank page. If you put this code at the top of your page it will display the errors that you have: ini_set ("display_errors", "1"); error_reporting(E_ALL); Like unska said, here is the solution, they HAVE TO choose an option from the drop down: <?php if(isset($_POST['submit'])) { echo $_POST['list']; } ?> <html> <head> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <select name="list"> <option value="car">car</option> <option value="bus">bus</option> <option value="motorbike">motorbike</option> </select> <br> <input type="submit" name="submit" value="Submit!"> </form> </body> </html> if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list" Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794133 Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 Because it was not specified. If you want it that way... modify a bit of the code with this: // replace all the code within if() bracket with this if(isset($_POST['submit'])) { if(empty($_POST['list'])) echo "please choose one from the list"; else echo $_POST['list']; } Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794138 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list" Maybe I'm confused but with a SELECT, the first option is automatically selected by default, so it's impossible for someone not to choose an option. Am I missing something...? Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794143 Share on other sites More sharing options...
bluejay002 Posted March 26, 2009 Share Posted March 26, 2009 I have tried with what Maq said and it did... it should have a default one right away even without specifying something. My previous post should be a bit nullified but might want to try it if it works for you. By the way, have you tried clicking the submit button? It should work that way. Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794152 Share on other sites More sharing options...
bal bal Posted March 26, 2009 Author Share Posted March 26, 2009 if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list" Maybe I'm confused but with a SELECT, the first option is automatically selected by default, so it's impossible for someone not to choose an option. Am I missing something...? yes..... this is the code for select <select name="list"> <option></option> <option value="car">car</option> <option value="bus">bus</option> <option value="motorbike">motorbike</option> </select> so 1st option is not selected. its still not working > Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794153 Share on other sites More sharing options...
bal bal Posted March 26, 2009 Author Share Posted March 26, 2009 if you don't select any thing from the list, why it is not showing any message, like "please choose one from the list" Maybe I'm confused but with a SELECT, the first option is automatically selected by default, so it's impossible for someone not to choose an option. Am I missing something...? yes..... this is the code for select <select name="list"> <option></option> <option value="car">car</option> <option value="bus">bus</option> <option value="motorbike">motorbike</option> </select> so 1st option is not selected. its still not working > when I tried with this code, it shows me "The page cannot be found" error Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794157 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Why do you have an empty option in your select field? Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794164 Share on other sites More sharing options...
bal bal Posted March 26, 2009 Author Share Posted March 26, 2009 Why do you have an empty option in your select field? if I don't put an empty option, & the user don't change it, that means the user choose the wrong option. thats why I want to make sure the user choose the right one and if it is blank, it should says "you have to choose one option from the list" Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794169 Share on other sites More sharing options...
Maq Posted March 26, 2009 Share Posted March 26, 2009 Why do you have an empty option in your select field? if I don't put an empty option, & the user don't change it, that means the user choose the wrong option. thats why I want to make sure the user choose the right one and if it is blank, it should says "you have to choose one option from the list" OK, doesn't make any sense to me but here: if(isset($_POST['submit'])) { if($_POST['list'] == "no") { echo "You didn't choose an option."; } else { echo $_POST['list']; } } ?> </pre> <form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post"> car bus motorbike </form> <b Quote Link to comment https://forums.phpfreaks.com/topic/151153-select-from-the-list/#findComment-794176 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.