payney Posted October 9, 2007 Share Posted October 9, 2007 Hi guys I have a variable with a value of either y or n. Now, I need my select list to default to this value depending on what it is. I am not sure how to do this. <?php $select = 'y'; ?> <select name="select"> <option value="y" selected>Yes</option> <option value="n">No</option> </select> Thanks all! Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/ Share on other sites More sharing options...
~n[EO]n~ Posted October 9, 2007 Share Posted October 9, 2007 <?php $select = 'y'; ?> <select name="select"> <option value="y" <? if($select=="y") echo "selected"; ?>>Yes</option> <option value="n" <? if($select=="n") echo "selected"; ?>>No</option> </select> Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365303 Share on other sites More sharing options...
MasterACE14 Posted October 9, 2007 Share Posted October 9, 2007 if you put it as the first <option it is automatically selected. Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365304 Share on other sites More sharing options...
payney Posted October 9, 2007 Author Share Posted October 9, 2007 <p>Display News: <? echo $row['display']; ?> <select name="display"> <option value="y" <? if($row["display"]=="y") echo "display"; ?>>Yes</option> <option value="n" <? if($row["display"]=="n") echo "display"; ?>>No</option> </select> Now, it works and gets the variable, but it dosnt set it to that value. Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365310 Share on other sites More sharing options...
ToonMariner Posted October 9, 2007 Share Posted October 9, 2007 surely a yes no should be a radio button group... if you wish to continue with the select box then echo "display"; should be echo ' selected="selected"'; Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365313 Share on other sites More sharing options...
payney Posted October 9, 2007 Author Share Posted October 9, 2007 It dosnt seem to work. The value I select is working. But when I enter the page, it is always selecting yes Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365319 Share on other sites More sharing options...
payney Posted October 9, 2007 Author Share Posted October 9, 2007 Please help guys Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365435 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2007 Share Posted October 9, 2007 Please post the whole script so we can see what you're trying to do. Ken Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365444 Share on other sites More sharing options...
payney Posted October 9, 2007 Author Share Posted October 9, 2007 Thanks, here it is. //First I have the query $query = "SELECT * FROM news_page WHERE pk= $pk"; $row = mysql_fetch_array($result); //Then my form <select name="display"> <option value="y" <? if($row["display"]=='y') echo "y"; ?>>Yes</option> <option value="n" <? if($row["display"]=='n') echo "n"; ?>>No</option> </select> Basically, when I enter the page, the value is always set to yes. Does that make any sense? Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365465 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2007 Share Posted October 9, 2007 Where is the variable "$pk" being set? Ken Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365507 Share on other sites More sharing options...
ToonMariner Posted October 10, 2007 Share Posted October 10, 2007 WHY ARE YOU COMPLETELY IGNORING THE HTML TO GET AN OPTION SELECTED???? <select name="display"> <option value="y" <? if($row["display"]=='y') echo ' selected="selected"'; ?>>Yes</option> <option value="n" <? if($row["display"]=='n') echo ' selected="selected"': ?>>No</option> </select> Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-365808 Share on other sites More sharing options...
payney Posted October 10, 2007 Author Share Posted October 10, 2007 Haha, because I am stupid. Thanks ToonMariner that solved it What is selected though? Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-366273 Share on other sites More sharing options...
ToonMariner Posted October 10, 2007 Share Posted October 10, 2007 take a wild guess and I bet you won't a million miles away. Link to comment https://forums.phpfreaks.com/topic/72439-selectmenu-list-in-with-php/#findComment-366326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.