Non sono sicuro di avere capito bene. Il codice che posto visualizza un testo dopo del change sulla seconda select.
Codice PHP:
<!DOCTYPE html>
<?php
$bdd = new PDO('mysql:host=127.0.0.1; dbname=tests','root','', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_LOCAL_INFILE => true));
//------ DEBUT
$query = "SELECT distinct(anno) FROM eventi order by anno";
$sth = $bdd->prepare($query);
$sth->execute();
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<style type="text/css">
</style>
<script>
function testoEvento(valore)
{
document.getElementById("testo_evento").innerHTML = '';
$.ajax
({
type: "POST",
url: "test9369c.php",
data: {id: valore},
dataType: "html",
success: function(dati_testo){
$("#testo_evento").html(dati_testo);
return;
},
error:function(msg){
alert('Errore : '+msg);
}
});
return;
}
function riempiSecondaSelect(valore)
{
document.getElementById("testo_evento").innerHTML = '';
if (valore != 0)
{
$.ajax
({
type: "POST",
url: "test9369b.php",
data: {anno: valore},
dataType: "html",
success: function(dati_seconda_select){
$("#div_eventi").html(dati_seconda_select);
testoEvento(document.getElementById("eventi").value);
return;
},
error:function(msg){
alert('Errore : '+msg);
}
});
}
// in caso di errore non metto niente !!!
document.getElementById("div_eventi").innerHTML = '';
return;
}
</script>
</head>
<body>
Anno :
<select id="anno" onclick="riempiSecondaSelect(this.value)">
<option value="0">Seleziona</option><?php
while (list($anno) = $sth->fetch(PDO::FETCH_NUM))
{?>
<option value="<?php print $anno;?>"><?php print $anno;?></option><?php
}?>
</select>
<br/>
<div id="div_eventi">
</div>
<div id="testo_evento">
</div>
</body>
</html>
test9369b.php
Codice PHP:
<?php
//---- controllo il valore del parametro "anno"
if (!isset($_POST['anno']))
{
//---- parametro non è stato trasmesso esco !
print "";
die();
}
$anno_eventi = $_POST['anno'];
//---- Controllo se valore numerico
if (!is_numeric($anno_eventi)) {
//---- dato non numerico esco !
print "";
die();
}
$bdd = new PDO('mysql:host=127.0.0.1; dbname=tests','root','', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_LOCAL_INFILE => true));
//------ Lista degli eventi
$query = "SELECT * FROM eventi where anno = $anno_eventi order by anno, id";
$sth = $bdd->prepare($query);
$sth->execute();?>
<select id="eventi" onchange="testoEvento(this.value)"><?php
while (list($anno,$id,$evento) = $sth->fetch(PDO::FETCH_NUM))
{?>
<option value="<?php print "evento_".$anno."_".$id;?>"><?php print $evento;?></option><?php
}
?>
</select>
test9369c.php
Codice PHP:
<?php
//---- controllo il valore del parametro "id"
if (!isset($_POST['id']))
{
//---- parametro non è stato trasmesso esco !
print "";
die();
}
//---- id ha la struttura seguente evento + _ + anno evento + _ + id
$id = $_POST['id'];
$array = explode('_',$id);
if (count($array) != 3)
{
//---- array non ha 3 posti
print "errore 1";
die();
}
$anno_eventi = $array[1];
$id_evento = $array[2];
//---- Controllo se valore numerico
if (!is_numeric($anno_eventi) || !is_numeric($id_evento)) {
//---- dato non numerico esco !
print "errore 2";
die();
}
$bdd = new PDO('mysql:host=127.0.0.1; dbname=tests','root','', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_LOCAL_INFILE => true));
//------ Lista degli eventi
$query = "SELECT * FROM eventi_testo where anno = $anno_eventi and id= $id_evento order by anno, id, riga";
$sth = $bdd->prepare($query);
$sth->execute();
while (list($anno,$id,$riga,$testo) = $sth->fetch(PDO::FETCH_NUM))
{?>
<?php print $testo;?><br/><?php
}
?>
Allego anche la tabella eventi_testo struttura e dati da importare in phpMyAdmin. Cambiare l'estensione passandola da txt a sql.