Adica am asa:
Auto >>
Imobiliare >> -- cand se alege asta o sa apara alt select cu
Inchirieri>>
Vanzari >> --iar apoi alt select cu Garsoniere >>
Apartamente>> etc
Codul este structurat in 3 fisiere:
categ.php
Cod: Selectaţi tot
<?php require_once("includes/db_connection.php"); ?>
<script type="text/javascript">
//AJAX
function showSubcateg1(str)
{
if (str=="")
{
document.getElementById("subcateg").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("subcateg").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","subcateg1.php?s1="+str,true);
xmlhttp.send();
}
</script>
<p style="margin:20px 0px 30px 20px; font-size:23px;">Alegeti categoria in care va fi adaugat anuntul:</p>
<div style="margin-left:20px;">
<?php
mysql_select_db("x", $con);
//Selecteaza categoriile
$c=mysql_query("SELECT * FROM categorii");
echo '<form action="" method="post">
<select name="categorie" onchange="showSubcateg1(this.value)" multiple="multiple" size="12" style="float:left;">';
while ($r=mysql_fetch_array($c))
{
//Se afiseaza categoria
echo '<option value="'.$r['id_categ'].'">'.$r['categorie'].' >></option>';
}
echo "</select>
</form>";
?>
</div>
<div id="subcateg" style="float:left; margin-left:10px;margin-top:0px;"><!--Aici se afiseaza formul cu subcategoriile-->
</div><br />
subcateg1.php
Cod: Selectaţi tot
<?php require_once("includes/db_connection.php"); ?>
<script type="text/javascript">
function showSubcateg2(str)
{
if (str=="")
{
document.getElementById("subcateg1").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp2=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp2.onreadystatechange=function()
{
if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
{
document.getElementById("subcateg1").innerHTML=xmlhttp2.responseText;
}
}
xmlhttp2.open("GET","subcateg2.php?s2="+str,true);
xmlhttp2.send();
}
</script>
<?php
mysql_select_db("x", $con);
$s1=$_GET["s1"];
$result = mysql_query("SELECT * FROM subcateg1 WHERE id_categ = '".$s1."'");
echo '<form action="" method="post" style="float:left">
<select name="subcateg1" onchange="showSubcateg2(this.value)" multiple="multiple" size="12" style="float:left;">';
while($s1 = mysql_fetch_array($result))
{
echo '<option value='.$s1['id_subcateg1'].'>'.$s1['subcateg1'].' >></option>';
}
echo '</select></form>';
?>
<div id="subcateg1" style="float:left; margin-left:10px;margin-top:0px; border:1px solid black;width:200px;height:200px;">
</div>
subcateg2.php
Cod: Selectaţi tot
<?php require_once("includes/db_connection.php"); ?>
<?php
mysql_select_db("ax", $con);
$s2=$_GET["s2"];
$result = mysql_query("SELECT * FROM subcateg2 WHERE id_subcateg1 = '".$s2."'");
echo "<form action=\"\" method=\"post\" style=\"float:left;\">
<select name=\"subcateg2\" multiple=\"multiple\" size=\"12\" style=\"float:left;\">";
while($sub2 = mysql_fetch_array($result))
{
echo "<option value=".$sub2['id_subcateg2'].">".$sub2['subcateg2']." >></option>";
}
echo "</select>
</form>";
?>
<div id="subcateg2" style="float:left; margin-left:10px;margin-top:0px;">
</div>
Dupa ce aleg categoria imi afiseaza subcategoria, dar apoi nu-mi mai afiseaza subcategoria subcategoriei.