Forum Moderators: coopster

Message Too Old, No Replies

form select dropdown menu

how to specify selected option based on text file value

         

ahmed24

10:14 pm on Jul 21, 2009 (gmt 0)

10+ Year Member



hey everyone,

i have a html form select menu like this in my php file:

<select name="level">
<option value="0"></option>
<option value="A">A - Excellent</option>
<option value="B">B - Very Good</option>
<option value="C">C - Satisfactory</option>
</select>

i have a text file called level.txt, this file will only contain a one line value for one of the options above. i just wanted to know how i can specify selected option based on the value of the text file?

so in other words, if in the level.txt file the value is "B" then the option B should have selected next to it

any suggestions?

thanks

rocknbil

3:34 am on Jul 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like

$selected = 'B'; // You've opened the text file and stored value in $selected

var $vals = Array (
'0' => 'Please Select', //Should put something in this option value
'A' => 'A - Excellent',
'B' => 'B - Very Good',
'C' => 'C - Satisfactory'
);

var $select = '<select name="level">';


foreach ($vals as $key=>$value) {
$select .= '<option value="' . $key . '";
if ($key==$selected) { $select .= ' selected'; }
$select .= '">' . $value . '</option>';
}

$select .= '</select>';

Add newlines and escapes on the quotes if you wish . . . .