PHP-对象使用错误?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>XMLHTTPRequest对象使用实例</title>
<script language="javascript">
var xmlHttp;
xmlHttp=create_obj();
function create_obj()
{
var xmlHttp;
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
}
function process()
{
name=document.getElementById("myname").value;
xmlHttp.open("GET","16-1.php?name="+name,true);
xmlHttp.onreadystatechange=handle_f;
xmlHttp.send(null);
}
function handle_f()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
result=xmlHttp.respnseText;
document.getElementById("message").innerHTML="<b><u>"+result+"<u></b>";
}
else
{
alert("执行过程中出现问题,服务器返回:"+xmlHttp.statusText);
}
}
}
</script>
</head>
<body>
请输入你的名称:
<p>
<input type="text" id="myname" >
<p>
<input type="button" value="提交" onclick="process()" >
<p>
<div id="message"></div>
</body>
</html>
<?php
header('Content-Type:text/html;charset=GB2312');
$arr[0]="孙悟空";
$arr[1]="小哪吒";
$arr[2]="葫芦娃";
$arr[3]="米老鼠";
$arr[4]="唐老鸭";
$arr[5]="机器猫";
if(in_array($arr,$_GET[name]))
{
echo $_GET[name]."存在于列表中!";
}
else
{
echo $_GET[name]."不存在列表中!";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我勒个去的....
把你的code debug了半天, 原来:
result=xmlHttp.respnseText;
写错了,应该是:
result=xmlHttp.responseText;
少写了一个o.
唉...
1.你的异步提交不能提交给同一php文件,提交之后本页面不会全部刷新。你需要异步提交给另一个页面,然后在从那页面返回数据给本页面。像你现在这样提交的话,会返回整个页面的HTML代码。
2. in_array()函数语法有错误,改成:
in_array($_GET['name'],$arr);
强烈建议数组$_GET['name']的这种访问形式
3. 建议你使用Jquery来实现异步提交
=========================分割线==========================
按你的要求,我用Jquery实现:
index.html页面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>XMLHTTPRequest对象使用实例</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script> <!--引入Jquery-->
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
$.get('./index.php', // 异步请求页面
{name:$("#myname").val()}, // 异步请求发送的数据,name=input输入
function(result){ // 请求成功时运行的回调函数
$("#message").html(result); // 显示请求返回数据
});
});
});
</script>
</head>
<body>
请输入你的名称:
<p>
<input type="text" id="myname" >
<p>
<input id="submit" type="button" value="提交" >
<p>
<div id="message"></div>
</body>
</html>
index.php页面实现:
<?php
header('Content-Type:text/html;charset=utf-8');
$arr[0]="孙悟空";
$arr[1]="小哪吒";
$arr[2]="葫芦娃";
$arr[3]="米老鼠";
$arr[4]="唐老鸭";
$arr[5]="机器猫";
if(in_array($_GET['name'],$arr))
{
echo "<strong>".$_GET['name']."</strong>存在于列表中!";
}
else
{
echo "<strong>".$_GET['name']."</strong>不存在列表中!";
}