juggysingh Posted March 15, 2010 Share Posted March 15, 2010 Hi, Is there anyway to retrieve the selected text value aswell as the value from a drop down list? <select name="dropdwn1" id="dropdwn1" size="1"> <option value="0" selected="selected">Choose</option> <option value="value1">text1</option> <option value="value2">text2</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/ Share on other sites More sharing options...
Rifts Posted March 15, 2010 Share Posted March 15, 2010 you would have to submit it to a php file and in the file have something like $_GET['selected'] i hope that helped a little sorry im pretty new Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026510 Share on other sites More sharing options...
juggysingh Posted March 15, 2010 Author Share Posted March 15, 2010 do you mean; $_GET['value1']; :?? If so that would not work as it would only bring back the value and not the text value which is 'text1' which is what i need! thanks anyway!!!! Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026514 Share on other sites More sharing options...
hcdarkmage Posted March 15, 2010 Share Posted March 15, 2010 I believe that the only way that can be done is if your drop down was populated by a database or an array, or if your value is the same as your text. If you just typed the values and options in there yourself, then it will only read the value portion of the drop down box and you lose the other information. <select id="dropdwn1" name="dropdwn1" size ="1"> <option value="0" selected="selected">Choose</option> <option value="value1">value1</option> <option value="value2">value2</option> </select> or $states = array( "AL"=>"Alabama", "AK"=>"Alaska", "AZ"=>"Arizona" ) <select name="state" id="state"> <option value="" selected="selected">--- Select State ---</option> <? foreach($states as $key => $value){ echo '<option value="'.$key.'">'.$value.'</option>'; } ?> </select> then you can get the information you wanted. Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026529 Share on other sites More sharing options...
juggysingh Posted March 15, 2010 Author Share Posted March 15, 2010 hi hcdarkmage, Thanks for your code, i have tested it using a form as an example, but the value i get once posted is still not the name but the value?? Please see my code below! Thanks <?php if (!isset($_POST['submit'])) { ?> <?php $states = array( "AL"=>"Alabama", "AK"=>"Alaska", "AZ"=>"Arizona" ) ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <select name="state" id="state"> <option value="" selected="selected">--- Select State ---</option> <?php foreach($states as $key => $value){ echo '<option value="'.$key.'">'.$value.'</option>'; } ?> </select> <input type="submit" name="submit"> </form> <?php } else { $state = $_POST["state"]; echo $_POST['state']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026589 Share on other sites More sharing options...
MatthewJ Posted March 15, 2010 Share Posted March 15, 2010 <?php if (!isset($_POST['submit'])) { ?> <?php $states = array( "AL"=>"Alabama", "AK"=>"Alaska", "AZ"=>"Arizona" ) ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <select name="state" id="state"> <option value="" selected="selected">--- Select State ---</option> <?php foreach($states as $key => $value){ echo '<option value="'.$value.'">'.$value.'</option>'; } ?> </select> <input type="submit" name="submit"> </form> <?php } else { $state = $_POST["state"]; echo $_POST['state']; } ?> Since you just want the value and the text to be the same, just echo the value ($value) in both places. Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026592 Share on other sites More sharing options...
juggysingh Posted March 15, 2010 Author Share Posted March 15, 2010 Hi MatthewJ, I cant use the option of having the text and value the same as each other as this form is being developed as an order form. Thus the value will need to be a number and the text the name associated with that value. Example; <select name="shirt_price" size="3"> <option value="15">long_sleeve</option> <option value="20">short_sleeve</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026598 Share on other sites More sharing options...
MatthewJ Posted March 15, 2010 Share Posted March 15, 2010 Select menus work by passing the value... aside from having them match, I think you're going to be stuck with one or the other. You could have the values match with the shirts too though.. then just have some code on the handling page that converts the selected item to its value like <select name="shirt_price" size="3"> <option value="long_sleeve">long_sleeve</option> <option value="short_sleeve">short_sleeve</option> </select> Then in the handler if($_POST['shirt_price'] == "long_sleeve") { $_POST['shirt_price'] == 15; } else { $_POST['shirt_price'] == 20; } etc. Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026606 Share on other sites More sharing options...
hcdarkmage Posted March 15, 2010 Share Posted March 15, 2010 Or you can do it this way: <?php $sleeves = array( "15"=>"Long Sleeve", "20"=>"Short Sleeve", ); if (!isset($_POST['submit'])) { ?> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <select name="shirt" id="shirt"> <option value="" selected="selected">--- Select Sleeve Type ---</option> <?php foreach($sleeves as $key => $value){ echo '<option value="'.$key.'">'.$value.'</option>'; } ?> </select> <input type="submit" name="submit"> </form> <?php } else { $shType = $_POST["shirt"]; echo "This should work ".$sleeves[$shType]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1026624 Share on other sites More sharing options...
juggysingh Posted March 19, 2010 Author Share Posted March 19, 2010 thats great!! many thanks hcdarkmage and all of you who have replied!!! Quote Link to comment https://forums.phpfreaks.com/topic/195337-get-text-value-from-drop-down-list/#findComment-1028443 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.