从AJAX收到未找到电子邮件的响应错误

时间:2019-11-10 21:34:45

标签: php jquery ajax mysqli

我正在尝试从mysqli中选择我手动插入的电子邮件email in mysqli

这就是我尝试从表格中选择电子邮件的方式。

$sql = $conn->query("SELECT id FROM users WHERE email='$email'");

基本上,我通过post方法发送json,如果消息状态为成功,则电子邮件字段为绿色,否则为红色。现在发生的是,即使我的电子邮件有效还是无效,我仍然收到消息状态0,并且它检测为错误,这就是我在浏览器中看到的信息

{"status":0,"msg":"Email not found"}

这是我的代码:

<?php

        if(isset($_POST['email'])){
            $conn = new mysqli("localhost",  "root",  "account");
            $email = $conn->real_escape_string($_POST['email']);

            $sql = $conn->query("SELECT id FROM users WHERE email='$email'");
            if($sql->num_rows > 0 ){
                exit(json_encode(array("status" => 1, "msg" => "Email sent to your inbox!")));
            }else{
                exit(json_encode(array("status" => 0, "msg" => "Email not found")));
            }
        }
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>

<body>
    <div class="container" style="margin-top:250px;">
        <div class="row justify-content-center">
            <div class="col-md-6 col-md-offset-3" align="center">
                <img src="https://scottleroymarketing.com/wp-content/uploads/2016/06/Hiring-a-good-logo-designing-company-to-create-your-logo-1.jpeg" style="width: 100%; max-width:300px; height: 200px;">
                <input class="form-control" id="email" required>
                <button type="button" class="btn btn-primary">Submit</button>
                <br><br>
                <p id="response"></p>
            </div>
        </div>
    </div>




    <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script type="text/javascript">
        var email = $("#email");
        $(document).ready(function() {
            $('.btn-primary').on('click', function() {
                if (email.val() != "") {
                    email.css('border', '1px solid green');
                    $.ajax({
                        url: 'index.php',
                        method: 'POST',
                        dataType: 'json',
                        data: {
                            email: email.val()
                        }, success: function(response){
                            if(!response.success){
                                $("#response").html(response.msg).css('color', "red");
                            }else{
                                $("#response").html(response.msg).css('color', "green");
                            }
                        }
                    })
                } else {
                    email.css('border', '1px solid red');
                }
            });
        });
    </script>
</body>

</html>

0 个答案:

没有答案