Jump to content

Archived

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

Mutley

Pre-select form options

Recommended Posts

Say if I have 3 form options:
apple
plum
lemon

When I select "plum" how can I make it so next time you view the form options, it is highlighted/selected?

Share this post


Link to post
Share on other sites
Basic html code would be:
<option value="plum" selected="selected">plum</option>

If you are using sessions you could use something like:
<option value="plum" <?php if (isset($_SESSION['myselection'])) echo selected=\"selected\">plum";?></option>

Share this post


Link to post
Share on other sites
If it a form by way of POST, you can do it like so.

[code=php:0]$options = array ( 'apple', 'plum', 'lemon' );

echo "<select name='fruit'>";

for ( $i = 0; $i < sizeof ( $options ); $i++ )
{
echo "<option value='" . options[$i] . "'" . ( isset ( $_POST['fruit'] ) && $_POST['fruit'] == $options[$i] ? " selected='selected'" : null ) . ">" . $options[$i] . "</option>";
}

echo '</select>';[/code]


me!

Share this post


Link to post
Share on other sites

×

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.