weixin_33701294 2013-03-26 21:30 采纳率: 0%
浏览 24

实时电子邮件可用性检查

I am trying to make live email availability check using 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. 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. 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.

This is my php code:

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

$(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. Thank you very much!

  • 写回答

2条回答 默认 最新

  • 喵-见缝插针 2013-03-26 21:36
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用