我想创建一个Ajax函数,作出基于来自PHP的返回变量的决定。我遇到的主要问题是,当我将它打印到html类时,返回var将正确显示,但是在ajax中的决定仍产生相同的结果false
。为什么它不工作:jquery Ajax成功数据与PHP返回变量
下面是PHP形式:
<form class="LogInForm" method="post" action="Auth.php" >
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="Email Address" value="<?php echo $_SESSION['email'];?>">
</div>
<div class="form-group">
<input type="password" class="form-control" name="pw" placeholder="password">
</div>
<button type="submit" class="btn btn-default btn-block">Submit</button>
及以下的jQuery脚本:
$('.LogInForm').on('submit', function(e){
var values = $(this).serialize();
// get value of action attribute
var desination = $('.RemoveProd').prop('action');
// get current location url
// prevent page from leaving to desination
e.preventDefault();
$.ajax({
url: desination,
type: 'post',
data: values,
success: function(data){
if(data == 1){;
alert("True");
$('.Test').html(data);
}
else{
alert("False");
$('.Test').html(data);
}
},
error: function(){
}
});
});
PHP函数:
function Auth()
{
//start session and compture login info
$pass = md5($_POST['pw']);
$user = $_POST['email'];
//connect to DB
$conn = Connect();
//Select all queries
$select = "SELECT Email, Password FROM customers WHERE Email ='".$user."' AND Password = '".$pass."'";
$results = $conn->query($select);
//search in db for user and passwrod, also store quires in var to store in sessions.
if ($results->num_rows > 0) {
$bool = true;
}
else{
$bool= false;
}
echo $bool;
}
什么情况是,如果我没有放入正确的密码,如果提醒我False
,并在提交按钮下告诉我什么是数据(这是null
)。当我输入正确的密码时,它仍会提醒我False
,但数据显示在提交按钮下方为1
。
还要注意何时绕过Ajax函数PHP函数可以正常工作。
更新 我能够得到这个工作。我所做的只是将php文件Auth.php移动到另一个目录,似乎解决了这个问题。我知道这不是一个文件路径问题。我感谢大家的时间,并感谢您回答并指出我的方向正确,并带有安全漏洞。
您设置一个AJAX网址为'无功desination = $托( '(” RemoveProd。')。行动'); ',具有类'RemoveProd'的元素在哪里?如果没有这样的元素,那么你没有通过网址到ajax ... – DouglasCamargo
试图比较你的反应作为一个字符串? 'if(data ==='1')' –
附注:'$ pass = md5($ _ POST ['pw']);'不是一种很好的密码验证方式。注意'password_verify()'和'password_hash()' – Rasclatt