You are here: Re: Drop Down Lists « PHP Programming Language « IT news, forums, messages
Re: Drop Down Lists

Posted by David Haynes on 05/16/06 22:40

Alec wrote:
> I have a drop down list with the town options 'Bury' and 'Ipswich' as
> shown below.
>
> When selecting one of the options, its value is passed to variable
> $townsearch.
>
> How do I change the drop down list so the option selected last time is
> then the current one shown in the drop down box?
>
>
> <body>
>
> <?php $townsearch = $_get['town']; ?>
>
> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
>
> <select size="1" name="town">
>
> <option>ipswich</option>
> <option>bury</option>
> &nbsp;
> </select>&nbsp;
>
> <input type="submit" value="GO" />
>
> </form>
>
> Many thanks
>
> Alec
>

Here is one way:
<?php $townsearch = $_GET['town']; ?>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<select name="town">
<?php
$choices = array('ipswitch', 'bury');
foreach( $choices as $choice ) {
if( $choice == $townsearch ) {
printf("<option selected>%s</option>\n", $choice);
} else {
printf("<option>%s</option>\n", $choice);
}
}
?>
</select>
<input type="submit" value="GO" />
</form>

Here is another:
<?php $townsearch = $_GET['town']; ?>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
<select name="town">
<?php
$choices = array('ipswitch', 'bury');
foreach( $choices as $choice ) {
printf("<option %s>%s</option>\n", ($choice == $townsearch) ?
'selected' : '', $choice);
}
?>
</select>
<input type="submit" value="GO" />
</form>

-david-

 

Navigation:

[Reply to this 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

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