Jump to content

[SOLVED] An easy way to automatically do <SELECT>


grissom

Recommended Posts

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

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

<?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>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.