Ajax:使用PHP检查电子邮件可用性

时间:2018-01-19 04:32:00

标签: javascript php ajax

如果我输入的电子邮件不在数据库中,如果电子邮件在数据库中,则始终与警报相同。我的编码出了什么问题?

enter image description here

我输入电子邮件'irsyadfahmy@gmail.com'并输入结果,而不是'电子邮件地址可以使用'应该'电子邮件地址已经在使用'。

this.nodes().to$().attr('excluded', 'true');

签email.php

    <html>
    <head>
    <!-- sweet alert --> 
        <link rel="stylesheet" type="text/css" href="css/sweetalert.css">
        <script type="text/javascript" src="js/sweetalert.min.js"></script>
    <!-- end sweet alert --> 
    <script type="text/javascript">
        $(document).ready(function(){
            $('#email').blur(function(){
                var email = $(this).val();
                $.ajax({
                    type    : 'POST',
                    url     : 'check-email.php',
                    data    : 'email='+email,
                    success : function(data){
                        if(data==0)
                        {
                        swal({
                            title: "Email Address can used",
                            text: "",
                            type: "success"
                        });     
                        }
                        else
                        {
                            swal({
                            title: "Email Address Already in Use",
                            text: "",
                            type: "warning"
                        }); 
                        }
                    },
                });
            });
        });
            </script>
    </head>
<body>
<form class="form-horizontal" method="POST" name="form">
<input type="email" name="email" id="email" class="form-control" required>
</form>
</body>
    </html>

结果: enter image description here

3 个答案:

答案 0 :(得分:1)

您需要使用echoprint将数据发送回ajax(在这种情况下我使用die())。我也倾向于使用json来回应。您应该检查电子邮件是否有效,但您应该绑定电子邮件值而不是将其注入sql字符串:

PHP:

<?php
include('libraries/config.php');
# Set a default response
$def    = ['alert'=>true];
# Remove empty values
$email  = trim($_POST['email']);
# First check this is an actual email
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
    die(json_encode($def));
# You should bind/prepare $email, not insert variable into string
$query = mysqli_query($conn, "SELECT COUNT(*) as count FROM `user_csr` WHERE `email` = '{$email}'");
# Check that the query succeeded and get the count
if($query)
    # Fetch the results of the count
    $result = mysqli_fetch_assoc($query);
# Write json respons
die(json_encode([
    # If succeeded, write the count
    'counted' => ($query)? $result['count'] : 0
]));

JavaScript的:

<script type="text/javascript">
    $(document).ready(function(){
        $('#email').blur(function(){
            var email = $(this).val();
            $.ajax({
                type: 'POST',
                url: 'check-email.php',
                data: 'email='+email,
                success: function(response){
                    // Parse response
                    response = JSON.parse(response);
                    // See if alert is set (email is not valid)
                    if(typeof response.alert !== "undefined") {
                        // Set an program alert
                        alert('A program error occurred.');
                        // Stop
                        return false;
                    }
                    var counted = response.counted;
                    swal({
                        title: (counted == 1)? "Email Address Already in Use" : "Email Address can used",
                        text: "",
                        type: (counted == 1)? "warning" : "success"
                    });
                },
            });
        });
    });
</script>

答案 1 :(得分:0)

作为您的程序方案,只需在成功函数中交换if body,此处为代码片段

                    success : function(data){
                    if(data==0)
                    {
                    swal({
                        title: "Email Address Already in Use",
                        text: "",
                        type: "warning"
                    }); 
                    }
                    else
                    {
                    swal({
                        title: "Email Address can used",
                        text: "",
                        type: "success"
                    });     

                    }
                },

答案 2 :(得分:0)

请查看此代码,其中我还有一些PHP代码和JS代码

<?php

include 'libraries/config.php';

$email   = isset($_POST['email'])?$_POST['email']:'';

if(!empty($email)){

   $cekdata=mysqli_query($conn,"SELECT * FROM user_csr WHERE email = '$email'");

   return $cekdata->num_rows;  // if email found it returns 1 else 0

}else{
   return 0;
}

最后,您需要检查响应中的返回值

<script type="text/javascript">

$(document).ready(function () {
    $('#email').blur(function () {
        var email = $(this).val(); 
        if(email==""){ 
            alert('Please enter email address'); 
            return false; 
        }
        $.ajax({
            type: 'POST',
            url: 'check-email.php',
            data: 'email=' + email,
            success: function (data) {
                if (data == 0)
                {
                    swal({
                        title: "Email Address can use",
                        text: "",
                        type: "success"
                    });
                } else
                {
                    swal({
                        title: "Email Already Exists",
                        text: "",
                        type: "warning"
                    });
                }
            },
        });
    });
});

</script>