簡體   English   中英

Ajax:使用PHP檢查電子郵件的可用性

[英]Ajax : check email availability with PHP

如果我輸入的電子郵件不在數據庫中,則始終與該警報在數據庫中的警報相同。 我的編碼有什么問題?

在此處輸入圖片說明

我輸入電子郵件“ 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>

入住email.php

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

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

結果: 在此處輸入圖片說明

您需要使用echoprint將數據發送回ajax die()在這種情況下,我使用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>

作為您的程序場景,只需在成功函數中交換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;
}

最后,您需要檢查返回值以作為響應

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM