Ajax用户名检查保持返回true(已发布代码)

时间:2017-10-13 22:53:07

标签: php ajax

下面我发布了代码!一切都会有所帮助!我从一个网站得到这个,因为我无法弄明白。它在用户名输入下工作并显示,但它总是说用户名可用。我甚至创建了一个用户'bob',当我输入用户名'bob'时,它说它可用。

的Ajax

<script type="text/javascript">

        var timeout = null;
        var subdomainOK = true;
        $(function () {



            $("form").submit(function () {

                if (!subdomainOK) { return false; }
                if (!$('#TermsCB').is(':checked')) {
                    $('#TermsValidation').html("You need to accept the terms and conditions");
                    return false;
                }

                if ($(this).valid()) {
                    $('[type=submit]').button('loading');
                }
                return true;
            });

        });

      function check_availability(){  

    //get the username  
    var username = $('#Subdomain').val();  

    //use ajax to run the check  
    $.post("inc/accountcheck.php", { username: username },  
        function(result){  
            //if the result is 1  
            if(result = 0){  
                //show that the username is available  
                $('#SubdomainValidation').html(username + ' is Taken');  
            }else{  
                //show that the username is NOT available  
                $('#SubdomainValidation').html(username + ' is Available');  
            }  
    });  

  }  
 </script>

 <?php
//connect to database  
mysql_connect('localhost', 'root', 'seadude246');  
mysql_select_db('socialdb');  

//get the username  
$username = mysql_real_escape_string($_POST['Subdomain']);  

//mysql query to select field username if it's equal to the username that we check '  
$result = mysql_query('select username from users where username = "'. $username .'"');  

//if number of rows fields is bigger them 0 that means it's NOT available '  
if(mysql_num_rows($result)>0){  
    //and we send 0 to the ajax request  
    echo 0;  
}else{  
    //else if it's not bigger then 0, then it's available '  
    //and we send 1 to the ajax request  
    echo 1;  
}
?>

1 个答案:

答案 0 :(得分:0)

在你的PHP代码中,传递给$_POST['']数组的密钥应该是用户名,而不是子域名,因为从你的javascript中,你通过ajax发送的密钥是用户名,用户名字段的id是Subdomain

所以你应该改为

$_POST['username']