Hi,
I am trying to amend the showHint Function from w3schools.
I adapted the basic script to allow multiple calls and I don’t have a permanent array so I made my php page create one from the table.
Have tried to play around with this a bit but I currently get nothing.
My script is:
[CODE]
<head>
<script type=”text/javascript”>
function showHint(str,FILE+str,ID){
if (str.length==0){
document.getElementById(ID).innerHTML=””;
return;
}
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.open(“GET”,FILE,true);
xmlhttp.onreadystatechange=function(){
if {xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(ID).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send()
}
</script>
</head>
<body>
<label>Search by name: </label>
<input type=”text” onkeyup=”showHint(‘getNames.php?q=’,’txtHintName’)” Value=”Search” />
<br />
<div id=”txtHintName”></div>
<br />
</body>
There will be more calls but I thought I would try and get one working first.
My Name.php is:
[CODE]
<?php
$con = mysql_connect(“localhost”,”user”,”password”);
if (!$con){
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db(“mydb”, $con);
$result=mysql_query(“SELECT * FROM members”);
while($row = mysql_fetch_array($result)){
echo “$a[]='” . $row[‘name’] . “‘;”;
}
$q=$_GET[“q”];
if (strlen($q) > 0){
$hint=””;
for ($i=0; $i<count($a); $i++){
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))){
if ($hint==””){
$hint=$a[$i];
}else{
$hint=$hint . ” , “.$a[$i];
}
}
}
}
if ($hint == “”){
$response=”no suggestion”;
}else{
$response=$hint;
}
echo $response;
?>
Any guidance would be fantastic.