简体   繁体   English

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

[英]How to check Email already exist in db using ajax?

I am after a code for getting ajax response for checking whether an email address is already entered in the database. 我正在寻找用于获取ajax响应的代码,以检查数据库中是否已输入电子邮件地址。 Here is my code snippet. 这是我的代码段。

Java Script: 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>

My Check.php file is as follows 我的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';
}
}
?>

Finally the HTML code 最后是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.. 请帮助我。

The problem why not getting any response is here with AjaxComplete function 为什么没有得到任何响应的问题在这里与AjaxComplete函数

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

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 我不确定为什么要使用它,但是即使没有必要,也可以不使用它而实际使用

without Ajaxcomplete function made some changes in code. 没有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 is deprecated, so should stop using it and start using MySQLi 注意: mysql_ *函数已被弃用,因此应停止使用它并开始使用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';
        }
}
?>

Change your first line of php code to : 将您的PHP代码的第一行更改为:

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

And so your updated Check.php file: 因此,您更新的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';
}
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript检查用户名/电子邮件是否已经存在 - check if username/email is already exist using javascript 使用codeigniter和AJAX检查数据库SQL中是否存在电子邮件 - check if email exist in database SQL using codeigniter and AJAX 如何使用Codeinighter和Ajax检查用户表中是否存在电子邮件 - How to I check if email exist in user table with codeinighter and ajax 如何使用 Javascript 检查页面中的元素是否已存在? - How to check if elements in a page already exist using Javascript? 如何使用 firestore 检查 id 是否已存在于 React 中? - How can I check if the id already exist in React using firestore? 如何使用 MERN 检查 email 是否已存在 - How do I check if email already exists using MERN 如何使用Javascript检查Firebase中是否存在电子邮件? - How do i check if an email exist in Firebase using Javascript? 如何使用node js express检查dynamoDB中是否存在用户名或email? - how to check if username or email exist in dynamoDB using node js express? 检查 MongoDB 中是否已存在用于注册的用户名/电子邮件的有效方法 - Efficient way to check if username/email already exist in MongoDB for registration 检查电子邮件是否已经存在错误未定义索引:user_id - Check if email already exist error undefined index: user_id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM