2013-05-19 2 views
0

여기에는 두 개의 필드가 있습니다. 이름은 &입니다. XMLHttpRequest를 사용하여 값을 반향하려고했지만 제출을 클릭하면 암호가 아닌 이름 만 표시됩니다. 내가 잘못한 곳을 찾도록 도와주세요.URL에 값을 전달할 때 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

사용 $password = $_GET['password']; 대신 $password = $GET['password'];

0

두 문제,

첫 번째 : 페이지에 로드가 완료되지 않은 동안 DOM을 요청했습니다.

둘째 : 이전 사용자가 지적한대로입니다. this $ 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; 

?> 
관련 문제