Jump to content


Photo

Pre-select form options


  • Please log in to reply
2 replies to this topic

#1 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 04 October 2006 - 10:32 PM

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?
~ Mutley.

#2 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 04 October 2006 - 10:38 PM

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>

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 04 October 2006 - 10:46 PM

If it a form by way of POST, you can do it like so.

$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>';


me!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users