尝试从数据库jquery中检索用户名

时间:2012-10-06 22:18:44

标签: php jquery html mysql ajax

    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是一个不同的文件。

1 个答案:

答案 0 :(得分:3)

您的输入中有一个大写'用户名'id属性,您在jquery中使用小写'username'选择它:

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']);  

将它们全部设置为小写或大写,你应该没问题