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 Link to comment https://forums.phpfreaks.com/topic/148844-solved-an-easy-way-to-automatically-do/ 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 Link to comment https://forums.phpfreaks.com/topic/148844-solved-an-easy-way-to-automatically-do/#findComment-781610 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/148844-solved-an-easy-way-to-automatically-do/#findComment-781612 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. Link to comment https://forums.phpfreaks.com/topic/148844-solved-an-easy-way-to-automatically-do/#findComment-781619 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 ! Link to comment https://forums.phpfreaks.com/topic/148844-solved-an-easy-way-to-automatically-do/#findComment-781628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.