Forum Moderators: coopster
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
$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>';
}
Add newlines and escapes on the quotes if you wish . . . .