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! Quote Link to comment 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> Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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"'; Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
payney Posted October 9, 2007 Author Share Posted October 9, 2007 Please help guys Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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 Quote Link to comment 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> Quote Link to comment 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? Quote Link to comment 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. 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.