A JM Posted August 14, 2009 Share Posted August 14, 2009 I want to read the selected "text" from a <SELECT> on my form and not the "value" how would I do that? All I can find is how to read the "value" and since my "value" and "text" are different the "text" is what I need. Thanks. A JM, Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/ Share on other sites More sharing options...
benphelps Posted August 14, 2009 Share Posted August 14, 2009 This cannot be done in PHP. Only the value is sent to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/#findComment-897816 Share on other sites More sharing options...
A JM Posted August 14, 2009 Author Share Posted August 14, 2009 duh! thanks for the reply... I realize that now. A JM, Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/#findComment-897819 Share on other sites More sharing options...
kratsg Posted August 14, 2009 Share Posted August 14, 2009 Do you mean... <SELECT> <option value="somevalue">sometext</option> </SELECT> Oddly enough, I have something that allows you to switch stuff :-D Made it myself. Just give your select an id and change it for the script. Then, when the form is submitted, just have it switch the text and values and then let the form submit. function shownamval() { var selection = document.getElementById('mySelect'); var selectedIndex = selection.selectedIndex; for (i=0;i<selection.length;i++) { var names = selection.options[i].innerHTML; var values = selection.options[i].value; selection.options[i].innerHTML = values; selection.options[i].value = names; } } If you only want the selected option to have the text/value switched, only: function shownamval() { var selection = document.getElementById('mySelect'); var selectedIndex = selection.selectedIndex; var names = selection.options[selectedIndex].innerHTML; var values = selection.options[selectedIndex].value; selection.options[selectedIndex].innerHTML = values; selection.options[selectedIndex].value = names; } Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/#findComment-897820 Share on other sites More sharing options...
Psycho Posted August 14, 2009 Share Posted August 14, 2009 Oddly enough, I have something that allows you to switch stuff :-D Made it myself. Just give your select an id and change it for the script. Then, when the form is submitted, just have it switch the text and values and then let the form submit. What is the point of that. If you want the "text" of the select field, then just make the value the same as the text. Trying to manipulate the values on submission is completely unnecessary and would fail for anyone without javascript enabled Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/#findComment-897831 Share on other sites More sharing options...
kratsg Posted August 14, 2009 Share Posted August 14, 2009 Oddly enough, I have something that allows you to switch stuff :-D Made it myself. Just give your select an id and change it for the script. Then, when the form is submitted, just have it switch the text and values and then let the form submit. What is the point of that. If you want the "text" of the select field, then just make the value the same as the text. Trying to manipulate the values on submission is completely unnecessary and would fail for anyone without javascript enabled I understand that, but I don't know exactly what he is doing (perhaps he cannot manipulate the values, but merely the text) so I just provide the poster with a way that will help him do what he wants instead of forcing him down the one path. Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/#findComment-897835 Share on other sites More sharing options...
A JM Posted August 14, 2009 Author Share Posted August 14, 2009 Thanks for the suggestion but the value and text must remain different. Is it possible to have the ID set to something different and can PHP read the ID on Submit? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/#findComment-898008 Share on other sites More sharing options...
aschk Posted August 14, 2009 Share Posted August 14, 2009 "Thanks for the suggestion but the value and text must remain different." - why? If you need the text then you might as well make the value and text the same. What could you possibly be using the text for where the value isn't a more pertinent piece of information? Quote Link to comment https://forums.phpfreaks.com/topic/170199-read-text-from-listbox/#findComment-898023 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.