I'm having a problem with Ajax. Nothing happen when I change my select value.
I have a div with the id textHint in order to print the result.
Here is my select :
<form>
<select id="choix" name="choix" onchange="showUser(this.value)">
<div class="tutorial_list">
<?php
$db = mysql_connect('localhost', 'root', 'root');
mysql_select_db('Projet',$db);
$sql = 'select NomPromo, NumPromo from Promo';
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
while ($data = mysql_fetch_array($req)){
echo'<option value="'.$data['NumPromo'].'">'.$data['NomPromo'].'</option>';
}
?>
</div>
</select>
</form>
Here's my script :
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
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","data.php?q="+str,true);
xmlhttp.send();
}
}
</script>
And here's my data.php :
<?php
$q = intval($_GET['q']);
$db = mysql_connect('localhost', 'root', 'root');
mysql_select_db('Projet',$db);
$sql = "select Nom, Prenom from User where Groupe ='".$q."'";
$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());
while ($data = mysql_fetch_array($req)){
echo $data['Nom'].' '.$data['Prenom'];
}
?>