简体   繁体   English

检查数据库jquery + php中是否已经存在电子邮件

[英]check if email already exists in database jquery +php

Hy I'm trying to validate the email address in a registration form. 您好,我正在尝试通过注册表单验证电子邮件地址。 I want to check if the address exists in the database; 我想检查数据库中是否存在该地址;

i have the following function: 我有以下功能:

function validateEmailRepeat(){
        // check if email exists in the database;
             var emailVal = $('#email').val();
             // assuming this is a input text field

            $.post('check_email.php', {'email' : emailVal}, function(data) {
                    if(data==1) 
                        {
                        email.addClass("error");
                        emailInfo.text("Adress exists");
                        emailInfo.addClass("error");
                        dataExist=1;
                            }

                    else {

                        email.removeClass("error");
                        emailInfo.text("Valid adress");
                        emailInfo.removeClass("error");
                        dataExist=0;
                            }
                        });

                        if(dataExist==1)
                         return false;
                         else
                         return true;

                    }

the check_email.php looks like this: check_email.php如下所示:

require_once('db.php');

if (isset($_POST['email'])){
$email=mysqli_real_escape_string($con,$_POST['email']);
$sql = "SELECT * FROM users WHERE `username`='".$email."'";
$select=mysqli_query($con,$sql) or die("fail");
$row = mysqli_num_rows($select);

if ($row >0) {

    echo 1;
}else echo 0 ;

}
else
echo "post error";

i'm new at this . 我是新来的。 So don't be to tough. 因此,不要太强硬。 thanks in advance. 提前致谢。

LE:Sorry ! LE:对不起! i forgot the question... the problem is that my function returns true if I define var dataExist outside of the function form $.post and if i let it like it is now in the function I get "dataExist is not defined " error and returns true 我忘记了这个问题...问题是,如果我在$ .post函数形式之外定义var dataExist,并且如果我现在就让它像现在在函数中那样,我的函数将返回true,并且出现“ dataExist未定义”错误,并且返回true

Le: the PHP returns the expected 1 and 0 just the way i wanted.. Le:PHP会按照我想要的方式返回期望的1和0。

LE: seems like defining the LE:好像在定义

var dataExist=0; var dataExist = 0;

outside the function validateEmailRepeat() resolved the issue. 函数validateEmailRepeat()外部的问题已解决。

if there's another way more elegant please tell me because this seems a little stupid to me. 如果还有另一种更优雅的方法,请告诉我,因为这对我来说有点愚蠢。

The problem is you cannot return the value into the called function, because ajax work asynchronously. 问题是您不能将值返回到调用的函数中,因为ajax是异步工作的。

                function validateEmailRepeat(){
                // check if email exists in the database;
                     var emailVal = $('#email').val();
                // assuming this is a input text field

                $.post('check_email.php', {'email' : emailVal}, function(data) {
                    if(data==1){
                        email.addClass("error");
                        emailInfo.text("Adress exists");
                        emailInfo.addClass("error");
                        dataExist=1;
                    }else{
                        email.removeClass("error");
                        emailInfo.text("Valid adress");
                        emailInfo.removeClass("error");
                        dataExist=0;
                    }
                        $('#some_input').val(dataExist);
                        validateUserName();
                    });
                }

                function validateUserName() {
                    var input =  $('#some_input').val();
                    if (input) {
                       alert("This username is already taken");// whatever
                    } else {
                         // whatever
                    }

                };

You are comparing the username with the email address. 您正在将用户名与电子邮件地址进行比较。 Is this how your DB works? 这是数据库的工作方式吗?

$sql = "SELECT * FROM users WHERE username ='".$email."'"; $ sql =“选择*来自用户的用户username ='”。$ email。“'”;

I bet you have a field in your database for the email itself, otherwise i suggest you do this. 我敢打赌,您在数据库中有一个用于电子邮件本身的字段,否则,我建议您这样做。 The query should look like this: 查询应如下所示:

$sql = "SELECT * FROM users WHERE email = '".$email."'";

Without the single quotes for the column. 没有该列的单引号。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM