|
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>
>
> </select>
>
> <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]
|