Php 在url上传递值时发生XMLHttpRequest错误
这里有两个字段:名称和密码。我一直试图使用XMLHttpRequest回显该值,但当我单击submit时,它只显示名称,而不显示密码。请帮我找出哪里出了问题 ------------Index.html--------------Php 在url上传递值时发生XMLHttpRequest错误,php,xmlhttprequest,Php,Xmlhttprequest,这里有两个字段:名称和密码。我一直试图使用XMLHttpRequest回显该值,但当我单击submit时,它只显示名称,而不显示密码。请帮我找出哪里出了问题 ------------Index.html-------------- First Ajax应用程序 功能测试(){ var-xmlhttp; if(window.XMLHttpRequest){ xmlhttp=新的XMLHttpRequest(); }否则{ xmlhttp=新的ActiveXObject(“Microsoft.xm
First Ajax应用程序
功能测试(){
var-xmlhttp;
if(window.XMLHttpRequest){
xmlhttp=新的XMLHttpRequest();
}否则{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
var uname=document.getElementById('username')。值;
var upassword=document.getElementById('userpassword')。值;
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4){
document.getElementById('results').innerHTML=xmlhttp.responseText;
}
}
url=“testform.php?name=“+uname+”&password=“+upassword;
open(“GET”,url,true);
xmlhttp.send();
}
姓名:
密码
结果
-----------------Testform.php---------------
使用
$password=$\u获取['password']
而不是$password=$GET['password']代码>两个问题
首先:您要求DOM在页面运行时获取元素
未完成加载
第二:正如前一位用户所指出的。这$GET['password']强>
是无效语法:
现在,将您的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
$name = $_GET['name'];
$password = $GET['password'];
echo $password."<br />";
echo $name."<br />";
?>
<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
if(isset($_GET)):
$name = $_GET['name'];
$password = $_GET['password'];
echo $password."<br />";
echo $name."<br />";
endif;
?>