用户使用jQuery检查可用性

用户使用jQuery检查可用性

本文介绍了用户使用jQuery检查可用性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查我的用户名是否可以使用jQuery注册,该脚本实际上没有错误,但是即使数据库中没有用户名记录,它也会一直显示" the username in use ".

i am trying to check my user name is if its available to register or not by using jQuery, the script actually worked with no errors but it keeps say " the username in use " even there is no username record in the database.

这是我的函数的php代码

here is the php code for my function

function checkavailabileUserName($UserName) {
if(isSet($_POST['userName']))
{
$username = $_POST['userName'];


$sql_check = mysql_query("SELECT userUserName FROM user WHERE userUserName='$username'") or die(mysql_error());

if(mysql_num_rows($sql_check))
{
echo '<font color="red">The username <STRONG>'.$username.'</STRONG> is already in use.</font>';
}
else
{
echo 'OK';
}

}

}

我在名为RegisterUserAction.php的页面上这样调用此函数

i call this function on a page called RegisterUserAction.php like this

checkavailabileUserName($UserName);

这里是JAVASCRIPT代码

HERE IS JAVASCRIPT CODE

pic1 = new Image(16, 16);
pic1.src = "loader.gif";

$(document).ready(function(){

$("#userName").change(function() {

var usr = $("#userName").val();

if(usr.length >= 3)
{
$("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');

    $.ajax({
    type: "POST",
    url: "RegisterUserAction.php",
    data: "userName="+ usr,
    success: function(msg){

   $("#status").ajaxComplete(function(event, request, settings){

    if(msg == 'OK')
    {
        $("#userName").removeClass('object_error'); // if necessary
        $("#userName").addClass("object_ok");
        $(this).html('&nbsp;<img src="accepted.png" align="absmiddle"> <font color="Green"> Available </font>  ');
    }
    else
    {
        $("#userName").removeClass('object_ok'); // if necessary
        $("#userName").addClass("object_error");
        $(this).html(msg);
    }

   });

 }

  });

}
else
    {
    $("#status").html('<font color="red">The username should have at least <strong>3</strong> characters.</font>');
    $("#userName").removeClass('object_ok'); // if necessary
    $("#userName").addClass("object_error");
    }

});

});

最后这里是该字段的HTML代码

AND FINALLY HERE IS HTML CODE FOR THE FIELD

<td><label for="userName"></label>
      <input type="text" name="userName" id="userName" onkeyup="twitter.updateUrl(this.value)" class="inn" />

      <div id="status"></div>
<script type="text/javascript">
$( function () {
twitter.screenNameKeyUp();
$('#user_screen_name').focus();
});</script>

我在哪里出错了?

推荐答案

您的代码有错误.您正在使用$ UserName作为函数checkavailabileUserName的参数,但是没有将该参数传递给函数.这将导致错误,因此只有您收到了使用中的用户名"消息.

There is a mistake in your code. You are using $UserName as an argument for the function checkavailabileUserName but you are not passing that argument to the function. That results in error and so only you were getting the message "Username In Use".

从函数和代码中删除$ UserName参数将完全正常工作.

Remove that $UserName argument from the function and code will work perfectly.

如果您可以使代码更整洁并使用jquery ajax调用和PHP代码,例如:

May be it will be good if you can make your code more clean and tidy and may use the jquery ajax call and PHP code like:

jQuery Ajax呼叫

jQuery(document).ready(function(){
     jQuery("#userName").change(function() {
        jQuery("#status").empty();
        var usr = jQuery("#userName").val();
        if(usr.length >= 3)
        {
            jQuery("#status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
            jQuery.ajax({
               type: "POST",
               url: "RegisterUserAction.php" ,
               data: "userName="+ usr,
               success: function(response){
                        if(response)
                        {
                            jQuery('#status').html('<img src="accepted.png" align="absmiddle"> <font color="Green"> Available </font>');
                            return true;
                        }
                        else
                        {
                            jQuery('#status').html('<font color="red">The username <STRONG>'+usr+'</STRONG> is already in use.</font>');
                            return false;
                        }

                    }
        });
    }
    else
    {
        jQuery("#status").html('<font color="red">The username should have at least <strong>3</strong> characters.</font>');
        return false;
    }
     });
});

PHP代码:

function checkavailabileUserName($checkavailabileUserName)
{
    if(isset($_POST['userName']))
    {
    $username = $_POST['userName'];

    $sql_check = mysql_query("SELECT userUserName FROM user WHERE userUserName ='$username'") or die(mysql_error());

    if(mysql_num_rows($sql_check)>0)
    {
        echo false;
    }
    else
    {
        echo true;
    }
   }
   else
   {
    echo false;
   }
}

这篇关于用户使用jQuery检查可用性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:33