ajax不会检查电子邮件的可用性

时间:2013-09-04 10:48:03

标签: php jquery ajax

我想查看电子邮件的可用性,但这里有些问题。在我有一个表格内:

<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';
    }
}

2 个答案:

答案 0 :(得分:4)

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

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

此外,您的PHP代码对注入攻击持开放态度。改为使用参数化查询。

答案 1 :(得分:2)

用下面的代码替换你的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这个永远不会返回的if (isset($_POST['inpMail']))

在php替换

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

{{1}}