How to make a SELECT keep its value on a results page


I have a site with a page where users can select an area / town from a dropdown, click Submit and they get taken to a page displaying properties for sale in that specified town. It all works fine except for one thing. When they get taken to the results page, the dropdown always displays the first town in the alphabet as opposed to the one they searched for.

So what you get is the right results (a list of properties for the town you searched on) but at the top of those results, a dropdown that still shows the first town in the list.

The form on the query page is:

<form action="propertysearch.html" method="post" name="propertysearch">    
<select name="_Location" size="1" width="30">
    <option value="Bristol">Bristol</option> 
    <option value="Clerkenwell">Clerkenwell</option>
    <option value="Southwark">Southwark</option>
    <option value="Greenwich">Greenwich</option>  
    <option value="Islington">Islington</option> 
    <option value="Norfolk">Norfolk</option>
    <option value="Overseas">Overseas</option>  
    <input type="submit" value="Find Properties in Chosen Area" name="propertysearch" />

Obviously I can’t used <option selected… because it depends on the value the user searched on. But if a user say, searches on Greenwich, ion the results page I want the dropdown to show Greenwich as the selected / visible value- and so on.

Any idea how this can be achieved?


Ignore, sorted it- using:

<option value=' . $location . '>' . $location . '</option> 

In the recipient page, i.e plugging PHP into it.

If you just want it to display the list with the selected entry showing instead of adding that entry a second time at the top then use PHP to add “selected” to the option that has the appropriate value.

I don’t see why you can’t use selected. Here’s a quick-and-dirty way to do it:

$location = "Islington";
$locations = array(

print("<select name=\\"_Location\\">\

foreach($locations as $value) {
 if($value != $location) {
  printf(" <option value=\\"&#37;s\\">%s</option>\
", $value, $value);
 else {
  printf(" <option value=\\"%s\\" selected>%s</option>\
", $value, $value);