|
Posted by Hilarion on 11/29/05 16:55
> I've got a country list which, after selecting and the 'submit' button
> is clicked, I need for the list to re-display the selected list element,
> the values are for stored database referencing only and not for display.
>
>
> <select name="country">
> <option value="<?php echo $country;?>" SELECTED><?php echo
> $country;?></option>
> <option Value="AF">Afghanistan</option>
> <option Value="AL">Albania</option>
> <option Value="DZ">Algeria</option>
> ....
> <option Value="ZW">Zimbabwe</option>
> </Select>
Try this:
<select name="country">
<option Value="AF"<?php if ($country=='AF') echo ' SELECTED'; ?>>Afghanistan</option>
<option Value="AL"<?php if ($country=='AL') echo ' SELECTED'; ?>>Albania</option>
<option Value="DZ"<?php if ($country=='DZ') echo ' SELECTED'; ?>>Algeria</option>
....
<option Value="ZW"<?php if ($country=='ZW') echo ' SELECTED'; ?>>Zimbabwe</option>
</Select>
Better one, which assumes that you have your country names and codes in
some array in this format:
$countries = array(
'AF' => 'Afghanistan',
'AL' => 'Albania',
'DZ' => 'Algeria',
...
'ZW' => 'Zimbabwe'
);
would be:
<select name="country">
<?php
foreach( $countries as $code => $name )
{
printf(
"<option value=\"%s\"%s>%s</option>\n",
htmlspecialchars( $code ),
(($code === $country)?' SELECTED':''),
htmlspecialchars( $name )
);
}
?>
</select>
Hilarion
[Back to original message]
|