我正在尝试使用 AJAX 来改进我的登录系统以在不刷新页面的情况下运行。我对ajax很陌生。我发现的教程都使用 GET。我不想使用get。这是我的代码:
login.php(我从中删除了 CSS 代码)
<html>
<script type="text/javascript" src = "login/loginJS.js"></script>
<body>
<center>
<div class="rounded">
<form method='POST' action = "processLogin.php">
Username:<input type="text" class = "input1" name = "username"/><br>
Password:<input type="password" class = "input1" name = "password"/><br>
Remember Me?<input type="checkbox" name = "remember"/?><br>
<?php
session_start();
echo'<p id="errorField" class="error"></p>';
?>
<input type="submit" value = "Login" class = "button" onclick='process()'/>
<b><p>New User? <a href="register.php">Register</a></p></b>
</form>
</div>
</center>
</body>
</html>
loginJS.js
xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if (window.ActiveXObject){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if(!xmlHttp){
alert("The XML Http Request Failed");
}else{
return xmlHttp;
}
}
function process(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
login = encodeURIComponent(document.getElementById("loginField").value);
xmlHttp.open("POST", "login/processLogin.php",true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send();
}else{
setTimeout('process()',1000);
}
}
function handleServerResponse(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("errorField").innerHTML = message;
}
}
}
进程登录.php
<?php
session_start();
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo "<response>";
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == '' or $password == '')
{
echo 'The username/password fields may not be blank.';
}else{
echo 'This is a test';
}
echo "</response>";
?>
所以我的问题是,我应该怎么做才能把输入文本和密码字段中的变量作为一个 post 变量,然后用 javascript 发送它。我只需要发送用户名和密码字段。要查看网站, http: //rukiryo.bugs3.com 网站是这样的。当我使用我的页面刷新方法时,登录按钮有效,但我无法弄清楚使其适用于非刷新的最后步骤。
谢谢,亚历克斯