username available or not using php ajax
<?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."'"
I think you are checking username, but you are using
userid
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>
精彩评论