jQuery检查是否采用了用户名

时间:2021-11-20 16:54:17

Ok, this is driving me nuts. I have tried a ton of other answers here, to no avail and I hate that I am having to post something that has been beaten to death, but I just cant get this to work.

好吧,这让我疯了。我在这里尝试了很多其他的答案,无济于事,我讨厌我必须发布一些被打死的东西,但我不能让它发挥作用。

I am checking my db to see if a username exists and no matter what I type in (whether existing or not), the return says the name is available. If I run a check and dump it to the screen, the correct returns are printed.

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

<?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:

使用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

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

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>

2 个解决方案

#1


0  

You're not formatting the data from JS to PHP correctly nor defining the method the proper way:

您没有正确地将数据从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");
        }
    }
});

You should also probably define the type of data you're expecting back and try to use JSON to communicate data from PHP to JS:

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

PHP:

PHP:

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

JS:

JS:

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

#2


-1  

What I'd do is in PHP use

我要做的是使用PHP

  @ob_start;

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

Then in Ajax use something like:

然后在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);
             }
        });

Keep in mind I did not probe this code...

请记住,我没有探测这段代码......

#1


0  

You're not formatting the data from JS to PHP correctly nor defining the method the proper way:

您没有正确地将数据从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");
        }
    }
});

You should also probably define the type of data you're expecting back and try to use JSON to communicate data from PHP to JS:

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

PHP:

PHP:

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

JS:

JS:

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

#2


-1  

What I'd do is in PHP use

我要做的是使用PHP

  @ob_start;

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

Then in Ajax use something like:

然后在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);
             }
        });

Keep in mind I did not probe this code...

请记住,我没有探测这段代码......