Salve a tutti ho iniziato da poco a programmare in php e sto cercando di implementare 2 select option che una varia al variare dell'altra. In pratica sono una select Regione e una Provincia, al variare di regione dovrebbe variare anche provincia, ma non riesco a capire come risolvere, posto il codice di quello che ho creato:
File index.php
codice:
<?php include ("configuredb.php");?>
<html>
<head>
<script type="text/javascript">
function showregione(str, pr)
{
if (str=="" || pr=="")
{
document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getregione.php?q="+str+"&pr="+pr,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$q = mysql_query("SELECT * FROM regioni GROUP BY regione ORDER BY regione ASC");
echo '<form>';
echo '<select class="regione" name="regione" id="regione" onchange="showregione(this.value)">';
//Visualizzo i record
while ($r = mysql_fetch_array($q)){
$r = stripslashes($r['regione']);
echo '<option value="'.$r.'">'.$r.'</option>';
}
echo ' </select>'; //Chiudo il select regione
//echo '</form>';
$q = mysql_query("SELECT * FROM regioni ORDER BY provincia ASC");
//echo '<form>';
echo '<select class="provincia" name="provincia" id="provincia" onchange="showregione(this.value)">';
//Visualizzo i record
while ($r = mysql_fetch_array($q)){
$r = stripslashes($r['provincia']);
echo '<option value="'.$r.'">'.$r.'</option>';
}
echo ' </select>'; //Chiudo il select regione
echo '</form>';
?>
<div id="txtHint">Lista </div>
</body>
</html>
File getregione.php
codice:
<?php
$q=$_GET["q"];
$pr=$_GET["pr"];
$con = mysql_connect('localhost', 'root', '');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("regioni", $con);
$sql="SELECT * FROM annunci WHERE regionecliente = '".$q."' AND provinciacliente ='".$pr."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Regione</th>
<th>ID</th>
<th>Provincia</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['regionecliente'] . "</td>";
echo "<td>" . $row['provinciacliente'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Spero in un vostro aiutino, anche perché non riesco a venirne fuori ho seguito diverse guide su google ma non ci capisco mai nulla e non ho trovato una guida esauriente.
Grazie