Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ajaXray : Multilevel subselect (jquery-1.3.x)</title>
<script language="javascript" src="jquery-1.3.2.min.js"></script>
<script language="javascript">
function makeSublist(cat_principal,tipo,isSubselectOptional,tipoVal)
{
$("body").append("<select style='display:none' id='"+cat_principal+tipo+"'></select>");
$('#'+cat_principal+tipo).html($("#"+tipo+" option"));
var cat_principalValue = $('#'+cat_principal).attr('value');
$('#'+tipo).html($("#"+cat_principal+tipo+" .sub_"+cat_principalValue).clone());
tipoVal = (typeof tipoVal == "undefined")? "" : tipoVal ;
$("#"+tipo).val(tipoVal).attr('selected','selected');
$('#'+cat_principal).change(function(){
var cat_principalValue = $('#'+cat_principal).attr('value');
$('#'+tipo).html($("#"+cat_principal+tipo+" .sub_"+cat_principalValue).clone());
if(isSubselectOptional) $('#'+tipo).prepend("<option value='none' selected='selected'> Selecciona </option>");
$('#'+tipo).trigger("change");
$('#'+tipo).focus();
});
}
$(document).ready(function()
{
makeSublist('tipo','marca', true, '');
makeSublist('cat_principal','tipo', false, '1');
});
</script>
</head>
<table border=0><tr><td>
<?php
//Afegim la coneixo
include_once("conex.php");
?>
<!---Aqui llistaries les primers categorias--->
<select id="cat_principal" name="categoria" style="width: 120px">
<?php
//Llistem tots els productes
$sql=("select cat_ID,catCategoria from categories;");
$query=mysql_query($sql,$db_link);
print ("<option selected='selected'></option>");
while($row = mysql_fetch_array($query)) {
$catID=$row["cat_ID"];
$catCategoria=$row["catCategoria"];
print ("<option class='$catID' value='$catID'>$catCategoria</option>");
}
?>
</select>
</td></tr>
<tr><td>
<select id="tipo" name="subcategoria" style="width: 120px">
<?php
//Llistem tots els sub-productes
$sql=("select id_cat,nom_subc,id_subc from sub_categories;");
$query=mysql_query($sql,$db_link);
while($row = mysql_fetch_array($query)) {
$idCat=$row["id_cat"];
$nomSubc=$row["nom_subc"];
$idsubc=$row["id_subc"];
print ("<option class='sub_$idCat' value='$nomSubc'>$nomSubc</option>");
}
?>
</select>
</td></tr>
<tr><td>
<select id="marca" name="marca" style="width: 120px">
<?php
//Llistem totes les marques
$sql=("select id_marca,id_subc,nom_marca from marca;");
$query=mysql_query($sql,$db_link);
while($row = mysql_fetch_array($query)) {
$id_subc=$row["id_subc"];
$id_marca=$row["id_marca"];
$nom_marca=$row["nom_marca"];
print ("<option class='sub_$id_subc' value='$nom_marca'>$nom_marca</option>");
}
?>
</select>
</td></tr>
</table>