grissom Posted March 10, 2009 Share Posted March 10, 2009 Hello all I've written a bit of PHP to echo out a bit of HTML on the fly to create a drop down list, a bit like this : echo " <SELECT NAME = 'mylist'> <OPTION VALUE = 'whisky'>Jack Daniels <OPTION VALUE = 'rum'>Havana Club <OPTION VALUE = 'beer'>Budweiser </SELECT>"; Ok that's going fab. Now I'd like to insert a SELECTED command into the HTML based on the value of a PHP variable For example, if I had a PHP variable $my_favourite_drink = 'beer' then when the HTML did the drop down list, it would automatically do <OPTION VALUE = 'beer' SELECTED>Budweiser when it got to the appropriate value based on whatever $my_favourite_drink was. I can think of loads of long-winded ways to do it, but there has to be a really smart way that doesn't involve loads of code. Help ! All the best and many thanks Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted March 10, 2009 Share Posted March 10, 2009 I answered something similar before even though it had some errors in it since I didn't test it. But the biggest shame is I miss spelled Chuck Norris. http://www.phpfreaks.com/forums/index.php/topic,239078.msg1114561.html#msg1114561 Quote Link to comment Share on other sites More sharing options...
trq Posted March 10, 2009 Share Posted March 10, 2009 <?php echo "<select name= 'mylist'>\n"; echo " <option value='whisky'" . ($my_favourite_drink == 'whisky' ? " selected='selected'>" : ">") . "Jack Daniels"; echo " <option value='rum'" . ($my_favourite_drink == 'rum' ? " selected='selected'>" : ">") . "Havana Club"; echo " <option value='whisky'" . ($my_favourite_drink == 'beer' ? " selected='selected'>" : ">") . "Budwieser"; echo "</select>"; ?> Quote Link to comment Share on other sites More sharing options...
grissom Posted March 10, 2009 Author Share Posted March 10, 2009 Thanks thorpe and Dj Kat. All your base ... that takes me back !! (nostalgic smile spreads across face ...) Take Off Every Zig For Great Justice. Anyway, thanks guys, that's excellent. Quote Link to comment Share on other sites More sharing options...
grissom Posted March 10, 2009 Author Share Posted March 10, 2009 Yup, just tried it, works a treat ! 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.