使用AJAX和PHP检查数据库中是否已存在电子邮件

时间:2014-03-25 16:25:45

标签: ajax database

因此,我尝试使用ajax onblur函数在电子邮件已被使用时发出错误,但似乎没有效果。

它似乎无法正确发送到getuser.php文件。有什么方法可以检查它是否真的正常通过了吗?

     <script type = "text/javascript">

            $('#email').blur(function() {
                newemail=($(this).val());
                var search = { email:newemail};
                $.ajax({
                    type:'POST',
                    url:'getuser.php',
                    data: search,
                    error:processError,
                    success: processData
                });
            });


       }
);

        function processError(textStatus, xhr) {
            alert("An error occurred: " + xhr.status + " - " + xhr.textStatus);
        }


        function processData(user) {
            if(user=="False")
            {
                if (! $('#fail').length) {
                    $('#email').after('<p id="fail">This email address has already been chosen - Please enter again</p>');
                }
                $("#email").css('background-color','#799');
                $("#email").focus();

            }
            else {
                if ($('#fail').length) {
                    $('#fail').remove();
                }
                $("#email").css('background-color','white');
            }
        }

    </script>

HTML

<div class ="form-group">
        <label class="col-md-4 control-label" for="name">Email</label>
        <div class="col-md-7">
            <input type="text" name="email" id="email" class="form-control"  required
                   pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$">
        </div>
    </div>

PHP

$email=$_REQUEST["email"];
            $sql="SELECT * FROM user WHERE email = :email";
            $stmt = $db->prepare($sql);
            $stmt->bindValue(':email', $email, PDO::PARAM_STR);
            $stmt->execute();
            $count= $stmt->rowCount();
            if ($count>0) {

                echo('False');

            }  else {

                echo('True');

            }

0 个答案:

没有答案