按php&;AJAX
我正在尝试编写一个程序,它要求用户输入一些字母或字符串,并通过这个字符串列出这些城市,这个字符串或字母是它们的前缀 我有以下html文件按php&;AJAX,php,ajax,Php,Ajax,我正在尝试编写一个程序,它要求用户输入一些字母或字符串,并通过这个字符串列出这些城市,这个字符串或字母是它们的前缀 我有以下html文件 <html> <head> <script> function showHint(str) { var xmlhttp; if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XML
<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>
函数showHint(str)
{
var-xmlhttp;
如果(str.length==0)
{
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“USA.php=“+str,true”);
xmlhttp.send();
}
输入您喜欢的任何城市名称
城市:
建议:
还有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中未定义的索引:city
但我不能理解这个错误,为什么城市指数是未定义的?
编辑:
html代码
函数showHint(str)
{
var-xmlhttp;
如果(str.length==0)
{
document.getElementById(“txtHint”).innerHTML=“”;
返回;
}
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,“USA.php?city=“+str,true”);
xmlhttp.send();
}
输入您喜欢的任何城市名称
城市:
建议:
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>
您在JavaScript中查询的字符串是错误的。更改以下行:
xmlhttp.open("GET", "USA.php?city="+str, true);
JavaScript中的查询字符串是错误的。更改以下行:
xmlhttp.open("GET", "USA.php?city="+str, true);
试试这个:
xmlhttp.open("GET","USA.php?city="+str,true);
好的,这就是问题所在。您请求获得关于USA.php
的提示,并在USA.php
中提交您的表格。然而,在请求提示列表时,您使用
xmlhttp.open(“GET”,“USA.php?city=“+str,true”)代码>
然而,您表单使用了post方法
问题出在您使用post的USA.php
上
$c=$\u POST['city']代码>
我建议你换一下ff
$c=$\u POST['city']代码>=>$c=$\u GET['city']代码>
=>
上次编辑:
把你的情况改成这个,我的朋友。你的一切都准备好了
if(strtolower($c)==strtolower(substr($line,0,$d))
在这里试试这个:
xmlhttp.open("GET","USA.php?city="+str,true);
好的,这就是问题所在。您请求获得关于USA.php
的提示,并在USA.php
中提交您的表格。然而,在请求提示列表时,您使用
xmlhttp.open(“GET”,“USA.php?city=“+str,true”)代码>
然而,您表单使用了post方法
问题出在您使用post的USA.php
上
$c=$\u POST['city']代码>
我建议你换一下ff
$c=$\u POST['city']代码>=>$c=$\u GET['city']代码>
=>
上次编辑:
把你的情况改成这个,我的朋友。你的一切都准备好了
<> >(StrutWar($C)=StrutWar(基($,0,$d))< /代码> < /p>谁能解释我为什么被否决了?你可能想考虑使用,那么你只需要担心PHP方面……谁能解释一下为什么会被否决?你可能想考虑使用,那么你只需要担心PHP方面……我已经改变了这条线,但代码再次表示city undefined Index您正在调用名为city的get变量,但您并没有声明它。这将解决您的错误问题。但是我已经在表单中指明了城市,所以我已经更改了get by post如果您将更改get to post,您还应该在post中更改表单的操作类型,并且您在php中调用它的方式应该是$\u post['city']
。我已经更改了这一行,但代码再次表示city undefined Index您正在调用名为city的get变量,但您并没有声明它。这将解决您的错误问题。但是我已经在表单中指明了城市,所以我已经更改了get by post如果您将更改get to post,您还应该在post中更改表单的操作类型,并且在php中调用它的方式应该是$\u post['city']
。