2017-10-13 76 views
-2

下面我發佈了代碼!任何事情都會有幫助!我從網站上得到這個,因爲我無法弄清楚。它工作並顯示在用戶名輸入下,但它總是會說用戶名是可用的。我甚至創建了一個用戶'bob',當我輸入用戶名'bob'時,它表示它可用。Ajax用戶名檢查一直返回true(代碼發佈)

阿賈克斯

<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; 
} 
?> 
+0

添加一個評論而不是一個答案 - 現在有點累得累了,不信任自己 - 是一個'if(result = 0)'應該是一個'if(result == 0)'? – Helpful

回答

0

在PHP代碼中,你傳遞給$_POST['']陣列的關鍵應該是用戶名,不是因爲你的JavaScript子域,關鍵你通過AJAX發送是用戶名和用戶名字段的ID是子域

所以你應該有

$_POST['username']

+0

你錯過了什麼 –

相關問題