伙计们,我正在尝试使用 Ajax 和 PHP 制作登录表单。每次提交表单时,我都会得到正确的响应,但这里的问题是我的 Javascript 文件中有一个“IF CONDITION”在我得到“成功”响应时没有执行。我不知道为什么。这是我的代码,请任何人帮助。
阿贾克斯文件
function ajaxObj( meth, url ) {
var x = new XMLHttpRequest();
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
if(x.readyState == 4 && x.status == 200){
return true;
}
}
Javascript 文件处理登录- 问题出在此文件中
function signin(){
var logstat = _("logstat");
var loge = _("SignInEmail").value;
var logp = _("SignInPasswordActive").value;
if(loge == "" || loge == "Email address" || logp == ""){
logstat.innerHTML = "Fill out all of the form data.";
} else {
_("loginbtn").style.display = "none";
logstat.innerHTML = 'please wait ...';
var ax = ajaxObj("POST", "signin.php");
ax.onreadystatechange = function() {
if(ajaxReturn(ax) == true) {
if(ax.responseText != "success"){
logstat.innerHTML = ax.responseText;
_("loginbtn").style.display = "block";
} else {
//Here is the problem I get success in Ajax Response, but this condition did not run
logstat.innerHTML = "login success";
_("loginbtn").style.display = "block";
}
}
}
ax.send("le="+loge+"&lp="+logp);
}
}
PHP 文件
<?php
if(isset($_POST['le']) && isset($_POST['lp'])){
$p = $_POST['lp'];
require_once('includes/database.php');
include_once('randStrGen.php');
$e = $_POST['le'];
$p_hash = md5($p);
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
if($e == "" || $e == "Email address" || $p == ""){
echo "Fill out all of the form data.";
exit();
} else {
$sql = "SELECT id, email, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
$query = mysqli_query($db->connection, $sql);
$numrows = mysqli_num_rows($query);
if($numrows == 0){
echo "This account is not exist";
exit();
}else{
$row = mysqli_fetch_array($query);
$db_id = $row['id'];
$db_em = mysqli_real_escape_string($db->connection, $row['email']);
$db_ps = substr($row['password'],10,-10);
if($p_hash != $db_ps){
echo "Email or Password combination failed";
exit();
}else{
//This is the successful Log in condition
echo "success";
exit();
}
}
exit();
}
exit();
}
?>