簡體   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