2013-05-19 23 views
0

這裏我有兩個字段:名稱&密碼。我一直試圖使用XMLHttpRequest來回顯該值,但是當我單擊提交時,它只顯示名稱而不顯示密碼。請幫我找出我出錯的地方。XMLHttpRequest URL上傳遞值出錯

------------的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

使用$password = $_GET['password'];,而不是$password = $GET['password'];

0

兩個問題,

第一:你問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文件,

<?php 
if(isset($_GET)): 
$name = $_GET['name']; 
$password = $_GET['password']; 

echo $password."<br />"; 
echo $name."<br />"; 


endif; 

?>