2012-10-06 57 views
0
echo "<form method='post' action='regprocess.php' id='registerform'>"; 
     echo '<fieldset class="register">'; 
     echo"<h2>Register</h2>"; 
      echo "<ul>"; 
        echo '<li><label for="FirstName">First Name: </label> <input type="text" name="FirstName" id="FirstName"></li>'; 
        echo '<li><label for="LastName">Last Name: </label> <input type="text" name="LastName" id="LastName"></li>'; 
        echo '<li><label for="Email">Email: </label><input type="email" name="Email" id="Email"></li>'; 
        echo '<li><label for="Username">Username: </label><input type="text" name="Username" id="Username"></li>'; 
        echo '<li><input type="button" id="check_username_availability" value="Check Availability"></li>'; 
        echo '<div id="username_availability_result"></div>'; 
        echo '<li><label for="Password">Password: </label><input type="password" name="Password" id="Password"></li>'; 
        echo '<li><input type="submit" value="Register"></li>'; 
        echo "</ul>"; 
     echo "</fieldset>"; 
     echo "</form>"; 


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 

<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT"> 
$(document).ready(function() { 

     var checking_html = 'Checking...'; 

     //when button is clicked 
     $('#check_username_availability').click(function(){ 
      //run the character number check 
       $('#username_availability_result').html(checking_html); 
       check_availability(); 
     }); 

    }); 

//function to check username availability 
function check_availability(){ 

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

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

} 
</script> 


<?php 
$conn = new mysqli('sapphire', 'cgreenheld', '', 'cgreenheld_dev'); 
$username = mysqli_real_escape_string($conn, $_POST['Username']); 

//mysql query to select field username if it's equal to the username that we check ' 
$usernameresult = 'Select Username from User where Username = "'. $username .'"'; 
$uresult = $conn->query($usernameresult); 

//if number of rows fields is bigger them 0 that means it's NOT available ' 
if($uresult->num_rows==1) { 
    //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; 
} 
?> 

大家好,我想檢查用戶名是在數據庫中,和我不知道相當有什麼錯我的代碼,當它從檢索的東西它只是總是顯示「未定義的數據庫不可用」,所以出於某種原因,它不會檢索用戶名,想知道是否有人可以幫助我?我真的被卡住了..這個php是在一個不同的文件中,只是爲了那些想知道的人。試圖從數據庫中檢索的jQuery用戶名

+0

在功能檢查console.log(結果)和chekc裏面打印什麼 –

+0

它沒有打印任何東西..我不認爲有錯誤。好吧,我的PHP錯誤日誌只有一個錯誤,只是一直說[07-Oct-2012 11:24:02] PHP注意:未定義的索引:用戶名在/devel/cgreenheld/projects/Asgn2mod/check_username.php在線3 但我不明白爲什麼它說,當我問人,他們似乎從來沒有告訴我爲什麼>。< – courtney

回答

3

您對您輸入大寫的「用戶名」 id屬性和您正在使用jQuery的一個小寫的「用戶名」中選擇:

echo '<li><label for="Username">Username: </label><input type="text" name="Username" id="Username"></li>'; 

var username = $('#username').val(); 

您發佈小寫的「用戶名」來PHP和試圖用一個大寫的「用戶名」

$.post("check_username.php", { username: username }, 

$username = mysqli_real_escape_string($conn, $_POST['Username']); 

將它們都以大寫或小寫,你應該確定訪問它

+0

對不起哪一個用​​戶名:用戶名應該設置爲大寫?第一個或第二個 – courtney

+0

首先。第一個是你的參數/名稱,第二個是你的值 - 你已經設置了var username = $('#username')。val()中定義的用戶名變量。 –

+0

非常感謝你:)但現在我得到500內部服務器錯誤,你說第二個是價值,這個大/小寫會讓我瘋狂。 – courtney