I have a set of 2 menus where the values of the 2nd are dependent on the value selected in the first. Yes, I will (eventually) convert this to a javascript process, but for now I am content reloading the page.
I have created a jumpmenu that reloads the page with the state passed in the url (ie. community.php?state=NY). This determines the values displayed in a second (dependent) menu which then lists the counties available for that state.
It is working nicely except that I am having difficulty is maintaining the selected value in the first menu after the reload.
Here is the code:
<?php $states = array (
'AL'=>'ALABAMA',
'AK'=>'ALASKA',
'AZ'=>'ARIZONA',
'AR'=>'ARKANSAS',
'CA'=>'CALIFORNIA',
'CO'=>'COLORADO',
'CT'=>'CONNECTICUT',
'DE'=>'DELAWARE',
'DC'=>'DISTRICT OF COLUMBIA',
'FL'=>'FLORIDA',
'GA'=>'GEORGIA',
'HA'=>'HAWAII',
'ID'=>'IDAHO',
'IL'=>'ILLINOIS',
'IN'=>'INDIANA',
'IA'=>'IOWA',
'KS'=>'KANSAS',
'KY'=>'KENTUCKY',
'LA'=>'LOUISIANA',
'ME'=>'MAINE',
'MD'=>'MARYLAND',
'MA'=>'MASSACHUSETTS',
'MI'=>'MICHIGAN',
'MN'=>'MINNESOTA',
'MS'=>'MISSISSIPPI',
'MO'=>'MISSOURI',
'MT'=>'MONTANA',
'NE'=>'NEBRASKA',
'NV'=>'NEVADA',
'NH'=>'NEW HAMPSHIRE',
'NJ'=>'NEW JERSEY',
'NM'=>'NEW MEXICO',
'NY'=>'NEW YORK',
'NC'=>'NORTH CAROLINA',
'ND'=>'NORTH DAKOTA',
'OH'=>'OHIO',
'OK'=>'OKLAHOMA',
'OR'=>'OREGON',
'PA'=>'PENNSYLVANIA',
'RI'=>'RHODE ISLAND',
'SC'=>'SOUTH CAROLINA',
'SD'=>'SOUTH DAKOTA',
'TN'=>'TENNESSEE',
'TX'=>'TEXAS',
'UT'=>'UTAH',
'VT'=>'VERMONT',
'VA'=>'VIRGINIA',
'WA'=>'WASHINGTON',
'WV'=>'WEST VIRGINIA',
'WI'=>'WISCONSIN',
'WY'=>'WYOMING');
?>
<select name="state" onChange="MM_jumpMenu('parent',this,0)" class="frmtxt2">
<option value="">-</option>
<?php
foreach($states as $key => $value) {
echo "<option value=community.php?state=$key";
if(isset($_GET['state']) && $_GET['state'] == $key) {
echo 'selected';
}
echo ">$value</option>";
} ?>
</select>
<?php
function build_county() {
if (isset($_GET['state'])) {
$state=$_GET['state'];
require_once ('x/mysql_connect.php'); // Connect to the database
$query = "SELECT DISTINCT(location_county) AS county FROM locations WHERE location_state = '$state' ORDER BY location_county ASC";
$result = @mysql_query($query);
while($row = @mysql_fetch_array($result, MYSQL_NUM)) {
$county = $row[0];
echo "<option value=\"$county\">$county</option>";
}
}
}
?>
<?php
if (isset($_GET['state'])) {
$state=$_GET['state'];
echo "<select name=county class=frmtxt2 id=county>
<option value=communities.php selected>-</option>";
echo build_county();
echo "</select>";
} else {
echo "Select A State";
}
?>
When looping through the states array and creating the menu I am using the same process I have used to make any menu hold the value with either POST or GET
if(isset($_GET['state']) && $_GET['state'] == $key) {
echo 'selected';
}
It continues to default back to the first value in the list after the reload. Any ideas?