tags:

views:

23

answers:

2

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

A: 

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

ItzWarty
Haven't used <select> and <option> for quite a while, sorry if I'm not correctly answering your question.
ItzWarty
+2  A: 

You have to add the selected attribute to the selected option, e.g.

<option value="foo" 
        <?php echo ($value=="foo") ? 'selected="selected"' : ''; ?> > 
    bar 
</option>
Felix Kling