Ho un form con due combo in cui una viene popolata dinamicamente usando ajax. Questo il codice della pagina con il form:
<?php
include "setup.php";
$db = mysql_connect($servidor, $usuario, $password);
mysql_select_db("$base",$db);
?>
<!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=iso-8859-1" />
<title>Select concatenate</title>
<script language="javascript">
var ricerca_pro;
function avviso() {
alert ("Attento ad Ajax");
}
function cerca()
{
var valorecampo = document.form1.codreg.options[document.form1.codreg.selectedIndex].value;
// per l'oggetto nativo XMLHttpRequest
if (window.XMLHttpRequest) {
ricerca_pro = new XMLHttpRequest();
ricerca_pro.onreadystatechange = ricevi;
ricerca_pro.open("GET", "cerca.php?codreg="+valorecampo,true);
ricerca_pro.send(null);
// per IE
} else if (window.ActiveXObject) {
ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
if (ricerca_pro) {
ricerca_pro.onreadystatechange = ricevi;
ricerca_pro.open("GET", "cerca.php?codreg="+valorecampo,true);
ricerca_pro.send();
}
}
}
function ricevi() {
var strRes;
var arrValori;
if (ricerca_pro.readyState == 4) {
if (ricerca_pro.status == 200){
document.getElementById('citta').innerHTML=ricerca _pro.responseText;
} else {
document.getElementById('citta').innerHTML="<selec t><option>ERRORE "+ricerca_pro.status+"</option></select>";
}
}
}
</script>
</head>
<body>
Scegli multiple:
<form id="form1" name="form1" method="post" action="ricevi.php" onsubmit="avviso()">
<label>regione
<select name="codreg" accesskey="1" tabindex="1" onchange="cerca()">
<option selected="selected" value=0>Scegli la regione</option>
<?php
$sql = "SELECT DISTINCT codreg,regione FROM regioni ORDER BY regione";
$result = mysql_query($sql);
while ($myrow=mysql_fetch_array($result)) {
echo "<option value=$myrow[codreg]>$myrow[regione]</option>";
}
?>
</select>
</label>
<label>
città
<div id=citta>
<select name="citta" accesskey="2" tabindex="2" onchange="avviso()">
</select>
</div>
</label>
</p>
<input type=submit value=invia>
</form>
</body>
</html>
e questo il codice della pagina cerca.php che si incarica di riempire la seconda select:
<?php
include "setup.php";
$db = mysql_connect($servidor, $usuario, $password);
mysql_select_db("$base",$db);
$sql = "SELECT * FROM province WHERE codreg='$codreg' ORDER BY provincia";
$result = mysql_query($sql);
echo "<select name=codprov onchange=\"avviso()\"><option selected=selected value=0>Scegli la provincia</option>";
while ($myrow=mysql_fetch_array($result)) {
echo "<option value=$myrow[codprov]>$myrow[provincia]</option>";
}
echo "</select>";
?>
Ora il problema è che con I.E. quando aggiorno la seconda select la funzione avviso() non si esegue mentre con firefox si esegue. Sembra che quando ajax va a scrivere la second select non si porti dietro anche la avviso(). Mi sapete spiegare la ragione?
P.S. Il tutto mi serve in quanto debbo costruire un sistema con select aa cascata che saranno 4 o 5 Grazie