如何使用ajax检查db中是否已存在电子邮件?

时间:2015-10-03 15:29:20

标签: javascript php jquery ajax

我正在获取一个代码,用于获取ajax响应,以检查是否已在数据库中输入了电子邮件地址。这是我的代码段。

Java脚本:

<script type="text/javascript">
pic1 = new Image(16, 16);
pic1.src="img/loader.gif";

$(document).ready(function(){

$("#email").change(function() {

var usr = $("#email").val();

if(usr.length >= 4)
{
$("#status").html('<img style="width:17px;" src="img/loader.gif" align="absmiddle">&nbsp;Checking availability...');

    $.ajax({ 
    type: "POST", 
    url: "check.php", 
    data: "email="+ usr, 
    success: function(msg){ 

   $("#status").ajaxComplete(function(event, request, settings){

    if(msg == 'OK')

    {
     alert ("error");

        $("#email").removeClass('object_error'); // if necessary
        $("#email").addClass("object_ok");
        $(this).html('&nbsp;<img src="tick.gif" align="absmiddle">');
    } 
    else 
    { 
        $("#email").removeClass('object_ok'); // if necessary
        $("#email").addClass("object_error");
        $(this).html(msg);
    } 

   });

 }

  });

}
else
    {
    $("#status").html('<font color="red">' +
'Enter Valid Email</font>');
    $("#email").removeClass('object_ok'); // if necessary
    $("#email").addClass("object_error");
    }

});

});
</script>

我的Check.php文件如下

<?php
// This is a sample code in case you wish to check the username from a mysql db table

if(isSet($_POST['email'])) {
$email = $_POST['email'];

$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '';
$dbDatabase = 'jaquar_cdb';

$db = mysql_connect($dbHost, $dbUsername, $dbPassword)
 or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db)
 or die ("Could not select database.");

$sql_check = mysql_query("select email from jaquar_contest where email='".$email."'")
 or die(mysql_error());

if(mysql_num_rows($sql_check)) {
    echo '<font color="red">The email <strong>'.$email.'</strong>'.
' is already in use.</font>';
} else {
    echo 'OK';
}
}
?>

最后是HTML代码

<div class="sf_columns column_3">                     
 <input id="email" type="text" placeholder="Email" name="email" data-required="true" data-email="true">
 <div style="left:0px;  padding-top: 10px;" id="status"></div>
</div>

问题是,我没有得到任何回应。我的错误是什么?请帮帮我......

2 个答案:

答案 0 :(得分:2)

为什么没有得到任何回复的问题在AjaxComplete function

$("#status").ajaxComplete(function(event, request, settings)

我不确定你为什么要使用它,但你可以不使用它,事实上即使你的方法没有必要

没有Ajaxcomplete function在代码中做了一些更改。

pic1 = new Image(16, 16);
pic1.src="img/loader.gif";
$(document).ready(function(){
    $("#email").change(function() {
    var usr = $("#email").val();
    if(usr.length >= 4){
        $("#status").html('<img style="width:17px;" src="img/loader.gif" align="absmiddle">&nbsp;Checking availability...');
        $.ajax({ 
        type: "POST", 
        url: "check.php", 
        data: "email="+ usr,
        dataType: 'text',
            success: function(msg){
                if(msg == 'OK'){
                 alert ("success");
                    $("#email").removeClass('object_error'); // if necessary
                    $("#email").addClass("object_ok");
                    $("#status").html('&nbsp;<img src="tick.gif" align="absmiddle">');
                } else {
                    alert ("error");
                   $("#email").removeClass('object_ok'); // if necessary
                   $("#email").addClass("object_error");
                   $("#status").html(msg);
               }
            }

        });
    } else {
        $("#status").html('<font color="red">' + 'Enter Valid Email</font>');
        $("#email").removeClass('object_ok'); // if necessary
        $("#email").addClass("object_error");
    }
    });
});

SideNote:mysql_* functions已被弃用,因此应停止使用它并开始使用MySQLi

<?php
// This is a sample code in case you wish to check the username from a mysql db table
if(isset($_POST['email'])) { //change isSet to isset (it will not make any difference)
    $email = mysql_real_escape_string($_POST['email']); //escape the string

    $dbHost = 'localhost'; // usually localhost
    $dbUsername = 'root';
    $dbPassword = '';
    $dbDatabase = 'jaquar_cdb';
    $db = mysql_connect($dbHost, $dbUsername, $dbPassword)
        or die ("Unable to connect to Database Server.");
    mysql_select_db ($dbDatabase, $db)
        or die ("Could not select database.");

    $sql_check = mysql_query("SELECT email FROM jaquar_contest WHERE email='$email'") or die(mysql_error());
        if(mysql_num_rows($sql_check) > 0) { //check rows greater then zero (although it will also not make any difference)
            echo '<font color="red">The email <strong>'.$email.'</strong>'. ' is already in use.</font>';
        } else {
            echo 'OK';
        }
}
?>

答案 1 :(得分:0)

将您的第一行PHP代码更改为:

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

所以您更新了 Check.php 文件:

<?php
// This is a sample code in case you wish to check the username from a mysql db table

if(isset($_POST['email'])) {
$email = $_POST['email'];

$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '';
$dbDatabase = 'jaquar_cdb';

$db = mysql_connect($dbHost, $dbUsername, $dbPassword)
 or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db)
 or die ("Could not select database.");

$sql_check = mysql_query("select email from jaquar_contest where email='".$email."'")
 or die(mysql_error());

if(mysql_num_rows($sql_check)) {
    echo '<font color="red">The email <strong>'.$email.'</strong>'.
' is already in use.</font>';
} else {
    echo 'OK';
}
}
?>