Hi guys and gals,

I am using the following code as a searchbox at the top of my site.

<form name="classic" method="post" action="error.php">
Quicksearch: <input name="INPUT" id="INPUT" type="text" value="" />
<select name="countries" onchange="updatecities(this.selectedIndex); document.classic.action=(this.value)">
<option value="" selected>Choose a category</option>
<option value="search1.php">Category One</option>
<option value="search2.php">Category Two</option>
<option value="search3.php">Category Three</option>
<option value="search4.php">Category Four</option>
</select>

<select name="cities">
</select>

<input type="image" name="search" src="search.png" />
</form>

<script type="text/javascript">

var countrieslist=document.classic.countries
var citieslist=document.classic.cities

var cities=new Array()
cities[0]=""
cities[1]=["A|Avalue"]
cities[2]=["B|bvalue", "C|cvalue", "D|dvalue", "E|evalue"]
cities[3]=["F|fvalue", "G|gvalue"]
cities[4]=["H|hvalue", "J|jvalue"]

function updatecities(selectedcitygroup){
citieslist.options.length=0
if (selectedcitygroup>0){
for (i=0; i<cities[selectedcitygroup].length; i++)
citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])
}
}

</script>

This gives me a different secondary dropdown box, based on what is selected in the first box, and then fires off what the user types into INPUT to the relevant search page.

This is included in a seperate php file, and included on every page of my site. My question is, is there a way, once the next page has been loaded to retain the drop down and secondary drop down that were selected by the user, rather that it resetting to the default 'Choose Category'?

There are several ways to do this. The easiest would probably be to use the session array to keep up with the option values. You could also set a cookie with javascript.

I'm not really an advanced coder. Are there any good tutorials I could use to learn how to implement a session array into the above code that you can recommend?

OK, I have found some code on making an array

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<select size="1" name="town">

<?php
$towns = array('ipswich','bury', 'eanske');
foreach($towns as $town){
$selected = (isset($_GET['town'])&&$_GET['town']==$town)? '
selected="selected"':'';
print("<option value=\"$town\"$selected>$town</option>");
} ?>


</select>
<input type="submit" value="GO" />
</form>

and it remembers the selected option from page to page. How would I insert this into the previous conditional dropdown menu so that it can remember the selected option across the conditional menu?

Am I just wildly on the wrong track here and there's a great example site with this exact menu on that I could use somewhere and I've been Googling for the wrong terms maybe?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.