Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/477.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax:使用PHP检查电子邮件可用性_Javascript_Php_Ajax - Fatal编程技术网

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

Javascript Ajax:使用PHP检查电子邮件可用性,javascript,php,ajax,Javascript,Php,Ajax,如果我输入了一封不在数据库中的电子邮件,如果该电子邮件在数据库中,则始终与该警报保持相同的警报。我的编码有什么问题 我输入电子邮件'irsyadfahmy@gmail.com'我输入结果,而不是'电子邮件地址可以使用'应该'电子邮件地址已经在使用' <html> <head> <!-- sweet alert --> <link rel="stylesheet" type="text/css" href="cs

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

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

    <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>

$(文档).ready(函数(){
$('#email').blur(函数(){
var email=$(this.val();
$.ajax({
键入:“POST”,
url:“check email.php”,
数据:“电子邮件=”+电子邮件,
成功:功能(数据){
如果(数据==0)
{
游泳({
标题:“可以使用电子邮件地址”,
正文:“,
类型:“成功”
});     
}
其他的
{
游泳({
标题:“电子邮件地址已在使用”,
正文:“,
类型:“警告”
}); 
}
},
});
});
});
check-email.php

<?php
include 'libraries/config.php';
$email   = $_POST['email'];

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

作为您的程序场景,只需在success函数中交换if主体,这里是代码片段

                    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"
                    });     

                    }
                },

请检查这段代码,我有一些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;
}

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

PHP:


您是否收到任何错误?在
控制台.log(数据)
之后,您在控制台中看到了什么?
<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>
<?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
]));
<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>