Reply to Re: List select query.

Your name:

Reply:


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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация