php利用ajax在本页面为什么不能获得值
<html>
<head>
<script src=j.js></script>
</head>
<body>
<input type=button name="username" onclick="Ajax_check_user_name()" value="点击">
<?php
echo $_GET[username];//为什么不能得到username值
?>
<div id="a">
</div>
</body>
</html>
js的文件
var http_request = false;
//用来建立HttpRequest对象
function CreateAjaxObject()
{
http_request = false;
if (window.XMLHttpRequest)
{ // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject)
{ // IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{}
}
}
}
//当服务器有回应时调用的函数
function alertContents()
{
var responseText;
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
responseText=http_request.responseText;
a.innerHTML="刘辉";//可以显示出来
//alert(http_request.responseText);
}
else
{
alert("There was a problem with the request.");
}
}
}
//实现AJAX异步的入口函数
function Ajax_check_user_name()
{
CreateAjaxObject();
if (!http_request)
{
alert("Giving up :( Cannot create an XMLHTTP instance");
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open("GET","index1.php?username="+username.value
+"&randNum="+Math.random(), true);
http_request.send(null);
} 展开
你这样写是不对的,我写两个参考代码,你看一下是不是你所需要的。
一:如果只是按你现在的要求在页面上显示结果的话,不需要PHP,也不需要AJAX
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>参考代码html</title>
</head>
<body>
<input type="button" name="username" value="点击" />
<div id="a"></div>
</body>
</html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("input").click( function () {
$("#a").text($(this).val());
});
</script>
二:如果你需要根据username的值做一些处理的话,这需要PHP,如果想要效果一样的,需要AJAX
代码二:
[test2.php]
<?php
$username = htmlspecialchars(trim($_GET['username']));
echo 'aaaaaaaaaaaaaaaaaaaaa<br />'.$username.'<br />bbbbbbbbbbbbbbb';
?>
[test.html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>参考代码html</title>
</head>
<body>
<input type="button" name="username" value="点击" />
<div id="a"></div>
</body>
</html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("input").click( function () {
$.get("test2.php", { username: $(this).val()},
function(data){
$("#a").html(data);
});
});
</script>
谢谢,我想知道php怎么利用ajax怎么在本页得到html里标签的值,能给我说说哪里错了吗
代码二就是了,代码写得比较简单,这样方便你理解,其中js部分是用jQuery框架的
下面是jquery的写法
var username = $("input[name=username]").val();
这样就可以了
<HTML的xmlns =“的1999/xhtml“目标=”_blank“> http://www.w3.org/1999/xhtml ”>
;