如何使用ajax检查数据库中已存在的电子邮件? [英] How to check Email already exist in db using ajax?

查看:104
本文介绍了如何使用ajax检查数据库中已存在的电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am after a code for getting ajax response for checking whether an email address is already entered in the database. Here is my code snippet.

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文件如下

My Check.php file is as follows

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

问题是,我没有得到任何答复.我的错是什么?请帮助我.

The problem is that, I am not getting any response.. What is my mistake? Kindly help me..

推荐答案

使用

我不确定您为什么要使用它,但是您可以不使用它而实际上并不使用它就可以做到

I'm not sure why you are using it but you can do without it and infact even it's not necessary with your approach

没有Ajaxcomplete function的代码进行了一些更改.

without Ajaxcomplete function made some changes in code.

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");
    }
    });
});

侧注: mysql_ *函数已弃用,因此应该停止使用它并开始使用MySQLi

SideNote: mysql_* functions is deprecated, so should stop using it and start using 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';
        }
}
?>

这篇关于如何使用ajax检查数据库中已存在的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆