这里我有两个字段:name&密码。我一直尝试使用XMLHttpRequest来回显该值,但是当我单击“提交”时,它只显示名称而不是密码。请帮我找出我出错的地方。
------------的index.html --------------
<html>
<head>
<title>Fist Ajax Application</title>
<script type="text/javascript">
function test(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var uname = document.getElementById('username').value;
var upassword = document.getElementById('userpassword').value;
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4){
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}
url = "testform.php?name="+uname+"&password="+upassword;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<table border="1">
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" id="userpassword" /></td>
</tr>
</table>
<input type="button" value="suubmit" onClick="test()" />
<p><div id="results"> Results...</div></p>
</body>
</html>
----------------- Testform.php ---------------
<?php
$name = $_GET['name'];
$password = $GET['password'];
echo $password."<br />";
echo $name."<br />";
?>
答案 0 :(得分:0)
使用$password = $_GET['password'];
代替$password = $GET['password'];
答案 1 :(得分:0)
两个问题,
首先:你问DOM,在页面有的时候获取元素 没有完成装载。
第二:正如之前的用户指出的那样。这 $ GET ['密码']; 是无效的语法:
现在,将您的Ajax
更改为:
<body>
<table border="1">
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" id="userpassword" /></td>
</tr>
</table>
<input type="button" value="suubmit" onClick="test()" />
<p><div id="results"> Results...</div></p>
<script type="text/javascript">
function test(){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var uname = document.getElementById('username').value;
var upassword = document.getElementById('userpassword').value;
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4){
document.getElementById('results').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","testform.php?name="+uname+"&password="+upassword, true);
xmlhttp.send();
}
console.log(test())
</script>
并且,您的PHP
文件
<?php
if(isset($_GET)):
$name = $_GET['name'];
$password = $_GET['password'];
echo $password."<br />";
echo $name."<br />";
endif;
?>