用户名可用或不使用php ajax

时间:2011-03-27 05:04:46

标签: php jquery mysql

<?php

if(isset($_POST[user_name]))
{
$user_name=$_POST[user_name];
include("include/conn.php");
$sql_check = mysql_query("select userid from `vector`.`signup` where userid='".$user_name."'")
 or die(mysql_error());

 //checking weather user exists or not in $existing_users array
if (mysql_num_rows($sql_check))
{   //user name is not availble
    echo "no";
} 
else
{
    //user name is available
    echo "yes";
}
}
?>

jquery代码

<script language="javascript">

$(document).ready(function()
{
    $("#username").blur(function()
    {
        //remove all the class add the messagebox classes and start fading
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
        //check the username exists or not from ajax
        $.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
        {
          if(data=='no') //if username not avaiable
          {
            $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
            { 
              //add message and change the class of the box and start fading
              $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
            });     
          }
          else
          {
            $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
            { 
              //add message and change the class of the box and start fading
              $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
            });
          }

        });

    });
});
</script>

我正在对用户名进行ajax验证是否存在,但此代码始终显示可用甚至用户名存在于数据库中。

如果没有用户名不可用,那么使用此代码我发送no和yes给jquery。

代码有什么问题吗?

3 个答案:

答案 0 :(得分:1)

  

vector选择用户ID。signup其中userid ='“。$ username。”'“

  1. 我认为您正在检查用户名,但您使用的是userid

  2. 您还有$user_name=$_POST[user_name];,但您使用的是$username

  3. <强>更新

    构建$sql语句。

     $sql = "select userid from `vector`.`signup` where userid='".$user_name."'";
    

    然后die($sql);

    您将能够在控制台上获得此查询作为响应。首先从mysql-console或phpmyadmin运行此查询,并确保获得结果。

答案 1 :(得分:0)

        <?php

        if(isset($_POST[user_name]))
        {
        $user_name=$_POST[user_name];
        include("include/conn.php");
        $sql_check = mysql_query("select userid from `vector`.`signup` where userid='".$user_name."'")
         or die(mysql_error());

         //checking weather user exists or not in $existing_users array
        if (mysql_num_rows($sql_check))
        {   //user name is not availble
            echo 0;
        } 
        else
        {
            //user name is available
            echo "yes";
        }
        }
        ?>

答案 2 :(得分:-1)

        <script language="javascript">

            $(document).ready(function()
            {
                $("#username").blur(function()
                {
                    //remove all the class add the messagebox classes and start fading
                    $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");
                    //check the username exists or not from ajax
                    $.post("user_availability.php",{ user_name:$(this).val() } ,function(data)
                    {
                      if(isNaN(data)) //if username not avaiable
                      {
                        $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
                        { 
                          //add message and change the class of the box and start fading
                          $(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
                        });     
                      }
                      else
                      {
                        $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
                        { 
                          //add message and change the class of the box and start fading
                          $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
                        });
                      }

                    });

                });
            });
            </script>