[AJAX] Ajax recherche par auto-complétion
Bonjour,
Je réalise un exercice d'entrainement avec l'ajax en utilisant le php et une base de donnée mysql mais la recherche ne fonctionne pas , je ne comprends pas d'où peut venir le problème.
Pourtant le code php fonctionne sans l'ajax il affiche bien ce que je veux.
voici mes codes :
Html + ajax
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>
<script type="text/javascript">
function initsearch(str){
var xmlhttp;
if(str.length==0){
document.getElementById("champ").innerHTML="";
return;
}
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readystate==4 && xmlhttp.status==200){
document.getElementById("ttt").innerHTML=xmlhttp.ResponseText;
}
}
xmlhttp.open("GET","getsearch.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<labal>entrer un numéro de client</label><input type="text" id="champ" size="15px" onkeyup="initsearch("this.value");"/>
<div id="ttt">
</div>
</body>
</html> |
Php
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?php
$q=$_GET["q"];
$con=mysql_connect("localhost","root","root");
if(!$con){
die('impossible de se connecter'.mysql_error());
}
mysql_select_db("qs",$con);
if(strlen($q)>0){
$result=mysql_query("select NOM from CLIENT where NCLI like '.$q.%' ");
$deb="<select multiple>";
while($row=mysql_fetch_array($result)){
$deb=$deb."<option>".$row['NOM']."</option>";
}
$fin=$deb."</select>";
}
echo $fin;
mysql_close($con);
?> |
Merci pour une aide parce que je bloque depuis un moment...