0

ここには、名前とパスワードの 2 つのフィールドがあります。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 />";


?>
4

2 に答える 2

0

2つの問題、

最初: ページの読み込みが完了していない間に要素を取得するように DOM に要求していました。

2番目:前のユーザーが指摘したとおりです。この$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;

?>
于 2013-05-19T15:02:25.493 に答える
0

$password = $_GET['password'];の代わりに使用$password = $GET['password'];

于 2013-05-19T11:26:40.183 に答える