so here it everything that is associated with the combobox
that ajax that we put in the page(its inside of the HEAD
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText;
}
}
};
req.open("GET", "locale.php?data="+src+"&val="+val);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
req.send(null);
}
window.onLoad=dochange('categoria', -1);
</script>
and combobox that we call in the same page
<?php
echo "<font id=categoria><select>\n";
echo "<option value='0'>Escolher</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=subcategoria><select>\n";
echo "<option value='0'>Escolher</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=subcategoria2><select>\n";
echo "<option value='0'>Escolher</option> \n" ;
echo "</select></font>\n";
?>
and the page sossociated
<?php
$data=$_GET['data'];
$val=$_GET['val'];
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "DB";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");
if ($data=='categoria') {
echo "<select name='categoria' onChange=\"dochange('subcategoria', this.value)\">\n";
echo "<option value='0'>== Escolher ==</option>\n";
$result=mysql_db_query($dbname,"select idcategoria,nomeCategoria from categoria");
while(list($idcategoria, $nomeCategoria)=mysql_fetch_array($result)){
echo "<option value=\"$idcategoria\" >$nomeCategoria</option> \n" ;
}
} else if ($data=='subcategoria') {
echo "<select name='subcategoria' onChange=\"dochange('subcategoria2', this.value)\">\n";
echo "<option value='0'>== Escolher ==</option>\n";
$val2=$val;
$val = substr($val,0,2);
$result=mysql_db_query($dbname,"SELECT idSubCategoria, nomeSubCategoria FROM subcategoria WHERE idcategoria='$val' order by idSubCategoria");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
} else if ($data=='subcategoria2') {
echo "<select name='subcategoria2' >\n";
echo "<option value='0'>== Escolher ==</option>\n";
$val2=$val;
$val = substr($val,0,4);
$result=mysql_db_query($dbname,"SELECT idSubCategoria2, nomeSubCategoria2 FROM subcategoria2 WHERE idSubCategoria='$val'");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
echo "</select>\n";
?>