ajax没有;t检查电子邮件是否可用


ajax doesn't check email availability

我想检查电子邮件的可用性,但这里有问题。在我的表格里:

<input id="inpMail" name="email" type="text" placeholder="E-mail">  

JS-

$(document).ready(function() {
    $("#inpMail").change(function() { 
        var mail = $("#inpMail").val();
        var msgbox = $("#status");  //`status` is a div
        if (mail.length > 4) {
            $("#status").html('<img src="img/loader.gif" align="absmiddle">&nbsp;Checking availability...');  //this works
            $.ajax({  
                type: "POST",  
                url: "ajax.php",  // this file is in the same folder
                data: "mail="+ mail,  
                success: function(msg) {  
                    $("#status").ajaxComplete(function(event, request){ 
                        if (msg == 'OK') { 
                            msgbox.html('<img src="img/available.png" align="absmiddle">');  //doesn't work
                        }  
                        else {  
                            msgbox.html(msg);  // doesn't work
                        }  
                    });
                } 
            }); 
        }
        else {
            $("#status").html('<font color="#cc0000">Please enter atleast 5 letters</font>');  //this works
        }
        return false;
    });
});

ajax.php

$conn = mysql_connect("localhost","root","") or die("Database not connected");  // this message never appears
$db = mysql_select_db("vedic", $conn) or die("Database not connected"); 
if (isset($_POST['inpMail']))
{
    $mail = $_POST['inpMail'];
    $sql = mysql_query("select id from members where email='$mail'"); 
    // my db is named `vedic`, table is `members` some fields are `id` and `email`  
    if (mysql_num_rows($sql))
    {
        echo '<STRONG>'.$mail.'</STRONG> is already in use.';
    }
    else
    {
        echo 'OK';
    }
}

您不需要在success回调中添加ajaxComplete处理程序,因为它已经发生了。试试这个:

success: function(msg) {  
    if (msg == 'OK') { 
        msgbox.html('<img src="img/available.png" align="absmiddle">');
    }  
    else {  
        msgbox.html(msg);
    }  
} 

此外,您的PHP代码对注入攻击非常开放。请改用参数化查询。

用下面的代码替换javascript,

$(document).ready(function()
{
    $("#inpMail").change(function() 
    { 
        var mail = $(this).val();
        var msgbox = $("#status");  //`status` is a div
        if(mail.length > 4)
        {
            msgbox.html('<img src="img/loader.gif" align="absmiddle">&nbsp;Checking availability...');  //this works
            $.ajax({
                type: "POST",  
                url: "ajax.php",  // this file is in the same folder
                data: "mail="+ mail,  
                success: function(msg)
                {  
                    if(msg == 'OK')
                    { 
                        msgbox.html('<img src="img/available.png" align="absmiddle">');  //doesn't work
                    }  
                    else  
                    {  
                        msgbox.html(msg);  // doesn't work
                    }  
                }
            });
        }
        else
        {
            $("#status").html('<font color="#cc0000">Please enter atleast 5 letters</font>');  //this works
        }
        return false;
    });
});

您在末尾有额外的});,而且您使用全局定义的相同选择器重复自己。

PHP

您发送的mail参数为张贴的email address,您正在检查inpMail,它永远不会返回true。

IN php替换

if (isset($_POST['inpMail']))

带有

if (isset($_POST['mail']))