I have a dropdown list being dynamically populated from an array but I can’t seem to get the syntax correct so that it displays the selected value after the form is submitted.
This is what I have so far..
<select id="country" name="country">
<option value="">Please Select</option>
<?php
foreach ($countrylist as $value => $country) {
echo '<option value="' . $value . '" ' . ($selected == $value ? ' selected' : '') . '>' . $country . '</option>';
} ?>
</select>
It seems that somewhere along the line I have made a mistake but I can’t seem to work out where.
As you are comparing each
$valuewith$selected,$selectedneeds to hold the value of the selected option. In this case it is hold in$_POST['country'].