|
Posted by Richard Brooks on 11/29/05 17:39
Hilarion wrote:
>> 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.
[snipped]
>
> 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
Thanks Hilarion!
I've got yet another variation of the above somewhere in my archive
tonnage but I had one of those 'elderly' moments and needed a quick answer.
Thanks again!
Richard.
Navigation:
[Reply to this message]
|