when loading my form (from variables) i can easily set the value of most text form items with the value tag开发者_运维知识库 like this:
<input type="text" name="fname" id="fname" value="<?php print "$fname";?>" />
how can i do the same with select input form elements? i guess i have to use a script?
thanks a lot
You have to add the selected
attribute to the selected option, e.g.
<option value="foo"
<?php echo ($value=="foo") ? 'selected="selected"' : ''; ?> >
bar
</option>
A select element is a parent of the option element, right?
If so, all you need to do is make the option have the selected attribute.
Instead of
<select>
<option>BLAAARRGGHHHH</option>
<option>ZAAAAAAAAARGGGH</option>
<option>NAARGHHH</option
</select>
you have
<select>
<option>BLAAARRGGHHHH</option>
<option selected="selected">ZAAAAAAAAARGGGH</option>
<option>NAARGHHH</option
</select>
to make "ZAAAAAAAAARGGGH" selected
精彩评论