繁体   English   中英

jQuery检查是否使用了用户名

[英]jQuery check if username is taken

好吧,这真让我发疯。 我在这里尝试了很多其他答案,但无济于事,我讨厌不得不张贴被殴打致死的东西,但我无法使它起作用。

我正在检查我的数据库,以查看用户名是否存在,无论我键入什么(无论是否存在),返回的信息都表明该名称可用。 如果我运行检查并将其转储到屏幕上,则会打印正确的退货。

<?php 
    $query = new Application_Model_Queries();
    $false = $query->userNames('vikingblooded');
    print_r( $false); //prints 1
    $true = $query->userNames('someonespecial');
    print_r($true); prints nothing

?>

Javascript:

$(document).ready(function () {
    $("#username").keyup(function () {
        $("#message").html("<img src='<?php echo $this->baseUrl('images/ajax-loader.gif'); ?>' class='status' /> checking...");
        var username = $("#username").val();
        $.ajax({
            method: "POST", 
            url: "<?php echo $this->baseUrl('users/check') ?>",
            data: {username: username}, 
            success: function (data) {
                if (data === 1) {
                    $("#message").html("<img src='<?php echo $this->baseUrl('images/cross.png') ?>' /> Username already taken");
                } else {
                    $("#message").html("<img src='<?php echo $this->baseUrl('images/tick.png') ?>' /> Username available");
                }
            }
        });

    });

});

的PHP

public function userNames($uName) {
    $select = $this->_dbTableUsers->select()
            ->from($this->_dbTableUsers, array('user_name'))
            ->where('user_name = ?', $uName);
    $rows = $this->_dbTableUsers->fetchRow($select);
    if ($rows) {
        return true;
    }
}

的HTML

<table>
        <tr>
              <td>Username</td>
              <td>:</td>
              <td><input type="text" name="username" id="username"/><td>
                <td id="message"><td>
        </tr>

        <tr>
              <td>Password</td>
              <td>:</td>
              <td><input type="text" name="password" id="password" /><td>
        </tr>
</table>

您没有正确地将数据从JS格式化为PHP,也没有正确定义方法:

$.ajax({
    method: "POST", // changed from type: "post"
    url:"<?php echo $this->baseUrl('users/check')?>",
    data: { username: username }, // changed from "username="+username
    success:function(data){
        if(data == 1){
            $("#message").html("<img src='<?php echo $this->baseUrl('images/cross.png')?>' /> Username already taken");                            
        } else {
            $("#message").html("<img src='<?php echo $this->baseUrl('images/tick.png')?>' /> Username available");
        }
    }
});

您还应该定义期望的数据类型,并尝试使用JSON将数据从PHP传递到JS:

PHP:

<?php echo json_encode(array('someValue' => false)); ?>

JS:

$.ajax({
    // other stuff
    dataType: 'json'
    // other stuff
});

我要做的是在PHP中使用

  @ob_start;

//your code
//...
//..
$return_arr["exists"] = 1; //or 0 if does not exist
ob_end_clean();                 
echo (json_encode($return_arr));

然后在Ajax中使用类似:

 $.ajax({
            type: "POST",
            url: url,
            data: data,
            dataType: dataType,
            success: function(data) {
                    if (data.exists == 1)
                   {
                      console.log('Exists');
                   } else {
                     console.log('nope');
                    }
                },              
            error: function (data){                 
                console.log(data);
             }
        });

请记住,我没有探究此代码...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM