Javascript PHP AJAX总是返回相同的结果

Javascript PHP AJAX总是返回相同的结果,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,因此,我试图检查电子邮件是否已经在使用中(密码重置)。所以我有我的JS //Check if email exists $(document).ready(function() { //listens for typing on the desired field $("#email").keyup(function() { //gets the value of the field var email = $("#email").val(); //here is w

因此,我试图检查电子邮件是否已经在使用中(密码重置)。所以我有我的JS

//Check if email exists
$(document).ready(function() {
//listens for typing on the desired field
$("#email").keyup(function() {
    //gets the value of the field
    var email = $("#email").val(); 

    //here is where you send the desired data to the PHP file using ajax
    $.post("../classes/check.php", {email:email},
        function(result) {
            if(result == 1) {
                //Email available
                console.log("Good");
            }
            else {
                //the email is not available
                console.log("Bad");
            }
        });
});
});
然后是我的PHP

<?php
//Include DB
include_once '../db.php';

if(isset($_POST['email'])){
    //Get data
    $email = htmlspecialchars($_POST['email'], ENT_QUOTES, 'UTF-8');
}
else{
    header('Location: /');
}
//Send requst to DB
$stmt = $con->prepare("SELECT * FROM users WHERE email = :email");
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->execute();

if($stmt->rowCount() > 0){
    //Email found
    echo 1;
}
else{
    //Email not found
    echo 0;
}

PDO文档警告说,
行数
可能不适用于所有驱动程序。更可靠、更有效的方法是:

$stmt = $con->prepare("SELECT COUNT(*) as count FROM users WHERE email = :email");
$stmt->bindValue(':email', $email, PDO::PARAM_STR);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row['count'] > 0) {
    echo 1;
} else {
    echo 0;
}
还有一件事要尝试:

$email = trim($_POST['email']);

因为有时输入字段中会有额外的空格。

您是否检查了控制台中的网络选项卡以查看服务器实际返回的响应?您是否尝试注销
结果
,其中包含哪些内容?此外,重复这样的数字也不是一个好主意,也不能使用弱相等来比较这些结果。在将数据存储到数据库中时,不要使用
htmlspecialchars
。只有在HTML页面中显示数据时才应使用该选项。是的,它返回的是
0
@barmar您正在尝试检查每个keyup上的电子邮件地址吗?对于攻击者来说,这无疑是一个很好的方法,可以在您的站点上构建一个包含所有可能的电子邮件组合的好词典。不,仍然抛出相同的问题。尝试修剪输入仍然没有更改。我还尝试了另一种浏览器来解决缓存问题。发生了同样的事情:(那么我认为你对数据库中的电子邮件的看法是错误的。我看不出任何失败的原因。我刚刚修复了SQL中的一个打字错误(
cont
=>
count
)。这可能是问题所在吗?