我正在尝试编写程序,它要求用户输入一些字母或字符串,并通过这个字符串,列出这些城市,这个字符串或字母是我有以下 html 文件的前缀
<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
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.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","USA.php="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<h3>entering any city names which you like </h3>
<form action="USA.php" method="post" >
CITY :<input type="text" name="city" value=" " onkeyup="showHint(this.value)"></br>
<input type="submit" value="Submit"/>
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
还有 USA.php 文件
<html>
<body>
<?php
$c=$_GET['city'];
$d=strlen($c);
$filename=file("UScities.txt");
$out=" ";
foreach ($filename as $line)
{
if (strtolower($c)==substr($line,0,$d)){
if($out=="")
{
$out=$line;
}
else
{
$out=$out. ' , ' . $line;
}
}
}
echo $out
?>
</body>
</html>
但是当我在Firefox中运行它时,它给了我以下错误
注意:未定义索引:第 4 行 C:\xampp\htdocs\united\USA.php 中的城市
但我无法理解这个错误,为什么城市未定义索引?编辑:
html code
<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
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.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "USA.php?city="+str, true);
xmlhttp.send();
}
</script>
</head>
<body>
<h3>entering any city names which you like </h3>
<form action="USA.php" method="post" >
CITY :<input type="text" name="city" value=" " onkeyup="showHint(this.value)"></br>
<input type="submit" value="Submit"/>
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
.php 文件
<html>
<body>
<?php
$c=$_POST['city'];
$d=strlen($c);
$filename=file("UScities.txt");
$out=" ";
foreach ($filename as $line)
{
if (strtolower($c)==substr($line,0,$d)){
if($out=="")
{
$out=$line;
}
else
{
$out=$out. ' , ' . $line;
}
}
}
echo $out
?>
</body>
</html>