【问题标题】:Trying to retrieve username from the database jquery试图从数据库 jquery 中检索用户名
【发布时间】:2012-10-07 06:35:33
【问题描述】:
    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 位于不同的文件中,仅供任何想知道的人使用。

【问题讨论】:

  • 在函数内部检查 console.log(result) 并检查它打印的内容
  • 它不打印任何东西。我认为没有错误。好的,我的 php 错误日志中有一个错误,它一直在说 [07-Oct-2012 11:24:02] PHP 注意:未定义的索引:第 3 行 /devel/cgreenheld/projects/Asgn2mod/check_username.php 中的用户名但是我不明白为什么会这样说,当我问别人时,他们似乎从来没有告诉我为什么>.

标签: php jquery html mysql ajax


【解决方案1】:

您的输入中有一个大写的“用户名”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']);  

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

【讨论】:

  • 对不起,哪个用户名:用户名我应该设置为大写?第一个或第二个
  • 第一。第一个是您的参数/名称,第二个是您的值 - 您已将其设置为 var username = $('#username').val(); 中定义的用户名变量;
  • 非常感谢 :) 但现在我收到 500 内部服务器错误,你说第二个是值,这个大写/小写字母会让我发疯。
  • 错误:未捕获的异常:[异常...“无法修改 WrappedNative 的属性”nsresult:“0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)”位置:“JS 框架 :: chrome://global/content/bindings /autocomplete.xml :: onxblpopuphiding :: line 864" data: no] 也有人知道这是什么意思..
  • Dw,我修好了 :) 非常感谢大家!
猜你喜欢
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-28
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多