开发者

username available or not using php ajax

开发者 https://www.devze.com 2023-02-20 05:30 出处:网络
<?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.\"\'\
<?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 code

<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 f开发者_StackOverflow中文版ading
              $(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);    
            });
          }

        });

    });
});
</script>

I"m doing ajax validation for username exists or not but this code always show available even username is exists in database.

using this code i sent no and yes to jquery if no username not available yes then available.

anything wrong with code ??


select userid from vector.signup where userid='".$username."'"

  1. I think you are checking username, but you are using userid

  2. Also you have $user_name=$_POST[user_name]; but you are using $username

UPDATE

construct a $sql statement.

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

then die($sql);

You will be able to get this query on your console as response. First run this query from mysql-console or phpmyadmin and make sure you are getting the results.


        <?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";
        }
        }
        ?>


        <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>
0

精彩评论

暂无评论...
验证码 换一张
取 消