<script type="text/javascript">
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
}
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 == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "gethint.php?q=" + str, true);
xmlhttp.send();
}
</script>
<form>
First name: <input type="text" onkeyup="showHint(this.value)" size="20" />
</form>
<span id="txtHint"></span>
-------------------------------------
<?php
$a[]="Anna";
$a[]="Wenche";
$a[]="Vicky";
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
答案 0 :(得分:3)
尝试更改此行
if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q))))
到
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
答案 1 :(得分:0)
if (xmlhttp == 4 && xmlhttp.status == 200) {
Should BE
if (xmlhttp.readyState==4 && xmlhttp.status==200)
---------------------------
if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q))))
Should BE
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
答案 2 :(得分:0)
我认为错误将在
上发生strtolower(substr($a[$a],0,strlen($q))))
这是什么$ a [$ a] ..?喜欢执行循环然后
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$a],0,strlen($q))))
请改变这样
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
$ a替换为$ i然后只有$ a []正在工作
答案 3 :(得分:-2)
<form>
FirstN<input type="text" onkeyup="return showHint(this.value);" size="20" />
</form>