Re: selected <option bla bla >

From: Date: Wed, 25 Apr 2001 23:26:20 +0000
Subject: Re: selected <option bla bla >
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-50383@lists.php.net to get a copy of this message
This probably isn't the most elegant method either, but this is how I did it: function mark_sel($value, $option) { // Compare the actual value ($value) to a possible value ($option) to determine // if the possible value should be marked as "selected" in a form element. if ($value == $option) return 'selected'; } In practice, it looks something like this: echo '<select name="jenis_biasiswa">'. '<option' . mark_sel($actual_value_in_db, Penuh) . '>Penuh</option>'. '<option' . mark_sel($actual_value_in_db, Separuh) . '>Separuh</option>'. '<option' . mark_sel($actual_value_in_db, Sendiri) . '>Sendiri</option>'. '</select>' ; A slightly more elegant method might be to write a function to write the entire option tag for you. Off the top of my head, that would probably be something like: function print_option($selected, $value) { // $selected is the value of the selected element, $value is the value of this // element. echo '<option'; if ($value == $selected) echo ' selected'; echo '>' . $value . '</option>' . "\n"; } If you want to get really fancy, write a print_selected function that accepts a list of options and loops through them to place the proper option tags, selections, etc. However, that's a bit beyond the scope of this message :) Note also that the label displayed to the user and the value sent to the server can be different if you want, by using the value attribute (e.g., <option value="foo">Click here to foo!</option>). -- Copyright 2001 Vivre Draco (cfc@bigfoot.com) My apologies if MS Outlook Express did anything evil to this message. I told it to post in plain text, but who knows if that means it'll actually DO it...

« previous php.general (#50383) next »