Hello all,

I have this 2 by 2 chained select drop downs. I want to make a dynamic 4 drop down menus with AJAX and PHP/mySQL, like this:

Category->dynamic subCategory

County->dynamic cityCounty

So far, when I test the Category and subCategory for itself and Count->cityCounty for itself, they work like a charm. But now when I try to combine them into one bigger form it's not working perfectly. What happend is that when I select Category, form reloads and subCategories show up while. Than I select subCategory and after that I select County wich causes cityCounty to load into 4th drop down, but than I get subCategory deleted. I don't know how to solve this. Here's the code:

Code JavaScript:
<script type="text/javascript">
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.zupanija.options[form.zupanija.options.selectedIndex].value;
var val3=form.subcat.options[form.subcat.options.selectedIndex].value;
self.location='final.php?cat=' + val +'&subcat=' +val3 +'&zupanija=' +val2;
}
</script>

And php:

PHP Code:
<?
@$cat=$_GET['cat']; 
@
$subcat=$_GET['subcat'];

$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 

if(isset(
$cat) and strlen($cat) > 0){
                                    
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory"); 
                                    }
else{
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); 

?>
<?
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>--- Kategorija ---</option>";
while(
$noticia2 mysql_fetch_array($quer2)) { 
    if(
$noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
        else{echo  
"<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
                                            }
echo 
"</select><br />";?>

<?
echo "<select name='subcat'><option value=''>Podkategorija</option>";
while(
$noticia mysql_fetch_array($quer)) { 
echo  
"<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo 
"</select>";
?>
<?

@$zupanija=$_GET['zupanija'];

$popisZupanija=mysql_query("SELECT DISTINCT imeZupanije, brZupanije FROM zupanije order by imeZupanije ASC"); 

if(isset(
$zupanija) and strlen($zupanija) > 0)
    {
    
$popisGradova=mysql_query("SELECT DISTINCT Grad FROM gradovi where brZupanije=$zupanija order by Grad"); 
    }

else{
    
$popisGradova=mysql_query("SELECT DISTINCT Grad FROM gradovi order by mbGrada"); 
    } 

echo 
"<select name='zupanija' onchange=\"reload(this.form)\"><option value=''>Županija</option>";
while(
$obavijest2 mysql_fetch_array($popisZupanija)) { 
if(
$obavijest2['brZupanije']==@$zupanija){echo "<option selected value='$obavijest2[brZupanije]'>$obavijest2[imeZupanije]</option>"."<BR>";}
else{echo  
"<option value='$obavijest2[brZupanije]'>$obavijest2[imeZupanije]</option>";}
}
echo 
"</select><br />";

echo 
"<select name='grad'><option value=''>Grad</option>";
while(
$noticia mysql_fetch_array($popisGradova)) { 
echo  
"<option value='$noticia[Grad]'>$noticia[Grad]</option>";
}
echo 
"</select>";
?>
I know that somewhere I made a mistake, but can't get it where..