简体   繁体   English

实时电子邮件可用性检查

[英]Live email availability check

I am trying to make live email availability check using ajax. 我正在尝试使用ajax进行实时电子邮件可用性检查。 I want to take the input value and send it to the php where it will be checked if it is in the database but I cannot make it. 我想获取输入值并将其发送到php,在其中将检查它是否在数据库中,但我无法输入。 When I test the scripts with Firebug, there is a response but the output is always the same whatever the email is existing or not. 当我使用Firebug测试脚本时,会有响应,但是无论电子邮件是否存在,输出始终是相同的。 It always shows me that is not existing. 它总是向我表明不存在。

The table is called company. 该表称为公司。 The table field for emails is Email. 电子邮件的表格字段是“电子邮件”。

The connection to the database is OK. 与数据库的连接正常。 I think the problem is in my ajax code because it is always giving me "TAKEN" output. 我认为问题出在我的Ajax代码中,因为它总是给我“ TAKEN”输出。

This is my php code: 这是我的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 = 'password';
$dbDatabase = 'php_test';

$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.");

$result = mysql_query("select Email from company where Email='".$email."'")
 or die(mysql_error());

if(mysql_num_rows($result))
{
echo 'NOT OK';
}
else
{
echo 'OK';
}
}
?>

This is the ajax code 这是ajax代码

$(document).ready(function() {

$("#email").blur(function(){
    $.ajax({  
    type: "POST",  
    url: "unique_check.php",  
    data: {Email: $("#email").val()},
    dataType:"json", 
    success: function(msg){

        if(msg == "OK"){
            alert("FREE")
        }else{
            alert("TAKEN")
        }
    }
    });
});    
}); 

Please, tell me where is my mistake or if there is a better way, give it. 请告诉我我的错误在哪里,或者如果有更好的方法,请解决。 Keep in mind that I am new at ajax and php. 请记住,我是ajax和php的新手。 Thank you very much! 非常感谢你!

I think you want: 我想你要:

if($result != false && mysql_num_rows($result) > 0) {
echo 'NOT OK';
} else {
echo 'OK';
}

If that doesn't work, try echoing out your query to make sure it is what you want. 如果那不起作用,请尝试回显查询以确保它是您想要的。

One issue is that in your Javascript code, you're setting email at page load, which happens to be empty. 一个问题是,在Javascript代码中,您是在页面加载时设置了email ,而email恰好是空的。 You need to set email again inside your .blur() function to grab the value when the field actually blurs. 您需要在.blur()函数中再次设置email以在字段实际模糊时获取值。

$(document).ready(function() {

    $("#email").blur(function(){
    var email = $("#email").val();
        $.ajax({  
            type: "POST",  
            url: "unique_check.php",  
            data: {Email: email},
            dataType:"json", 
            success: function(msg){

                if(msg == "OK"){
                    alert("FREE")
                }else{
                    alert("TAKEN")
                }
            }
        });
    });    
}); 

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

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