0

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

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

2 回答 2

4

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

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

此外,您的 PHP 代码很容易受到注入攻击。请改用参数化查询。

于 2013-09-04T10:51:54.793 回答
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哪些参数永远不会返回 true。

在 php 替换

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

if (isset($_POST['mail']))
于 2013-09-04T10:54:09.893 回答