Setting default value of a drop-down menu in HTML

Soldato
Joined
6 Jun 2005
Posts
2,668
Location
Wirral, UK
Hi guys,

I'm trying to set the default value of a HTML drop-down menu to the value of a PHP variable. The HTML for the menu looks something like this:

Code:
<select size="1" name="cmbSmoker">
<option>-</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>

I know that to set the default value I would add the "SELECTED" tag in like so:

Code:
<option value="Yes" SELECTED>Yes</option>

But how would I set the default value to the value of a variable? For example, if $smoker = 'Yes' then the default value of the drop-down menu would be "Yes".

To achieve the same sort of thing with a text box I have done the following:

Code:
<input type="text" name="txtSmoker" size="5" value=<?php print $smoker ?>>

But I can't see any easy way to do it with a drop-down menu.

Any suggestions much appreciated.
 
Thanks guys :)

I thought I'd probably end up having to put IF statements in each option. I've done it and it works great.

Moredhel, I've saved your code incase I decide I need more inputs. It's slightly overkill for what I have now but it looks great for a bigger project ;)

Thanks again.
 
Back
Top Bottom