Check username availability from database in php using jQuery
Hell Friends
here is an example to Check username availability from database in php using jQuery. This will be done with three steps.
here is an example to Check username availability from database in php using jQuery. This will be done with three steps.
- First create a file "index.php" or whatever you want.
- Create a "user_availability.php".
- Create database named "test" and table named "login".
What is jquery?
JQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript.index.php(This is the file from where you will input username)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Username availability Checking using jQuery</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script src="jquery-1.3.2.min.js" type="text/javascript" language="javascript"></script>
<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>
<style type="text/css">
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
.top {
margin-bottom: 15px;
}
.messagebox{
position:absolute;
width:100px;
margin-left:30px;
border:1px solid #c93;
background:#ffc;
padding:3px;
}
.messageboxok{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #349534;
background:#C9FFCA;
padding:3px;
font-weight:bold;
color:#008000;
}
.messageboxerror{
position:absolute;
width:auto;
margin-left:30px;
border:1px solid #CC0000;
background:#F7CBCA;
padding:3px;
font-weight:bold;
color:#CC0000;
}
</style>
</head>
<body>
<br>
<br>
<div align="center">
<div class="top" >
Please move the focus out of the box to check the availability of username
</div>
<div >
User Name : <input name="username" type="text" id="username" value="" maxlength="15" />
<span id="msgbox" style="display:none"></span>
</div>
</div>
</body>
</html>
user_availability.php
if(isset($_POST['username'])) {
$username = mysql_real_escape_string($_POST['username']);
if(!empty($username)) {
$username_query = mysql_query("SELECT COUNT(`id`) FROM `login` WHERE `username` = '$username'");
$username_result = mysql_result($username_query,0);
if($username_result == 0) {
echo 'yes';
} else ($username_result == 1) {
echo 'no';
}
}
}
Database script:
CREATE TABLE `login` (
`id` bigint(20) NOT NULL auto_increment,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`phoneno` bigint(15) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
INSERT INTO `login` (`id`, `username`, `password`, `email`, `phoneno`) VALUES
(1, 'ankit', '123', 'ankit@gmail.com', 9953471700);
i think this code it good but Why i try this code but it like no function?please help me because it's urgent
ReplyDeletethanks...problem solve...this source code is succesful....thanks you very much :)
ReplyDeleteThis code is very helpful...Thank You very much!..it really helps my school project...
ReplyDeleteplease help me guys....it always says username available...even the username already exist in the database
ReplyDeleteno my friend i have checked the script again and it is correct. It is giving me "This User name Already exists" message when username is already exit and "Username available to register" if name not found in database
ReplyDeleteI've trawled Google looking for a working copy of this script yet without fail all I can find like this example are ones that do not work...
ReplyDelete(I really wish people would check their scripts before posting them to be honest)
I did manage to find one example working on a site called betadistribution.com but since I can't see the user_availability.php script then I have no clue how they got around the database issue.
This one like all the others is a lemon.
Hi Mickelodian
ReplyDeleteSee user_availability.php in the code above, i have written the file code that you have to write in user_availability.php
The share box is irritating for 1024px DUDE
ReplyDeletePLZ Make the correction i can't read anything.
hi ankit.. i have a problem .. i need to check whether the user id is already available using AJAX and javascript.. i dono anyting about jquery .. iv been searching a lot but everywhere im finding the code which ues jquery.. please help... thank you
ReplyDeleteHi ankit..
ReplyDeleteI want small help.
Instead of text box can you help me with drop down list.
My drop down list contains timing field for two hours interval(8-10,10-12,12-2,2-4,4-6,6-8). I tried but it is not displaying any thing.
Thank you.
hi ankit
ReplyDeletei Had a problem,
Instead of text box can you help me with drop down list.
My drop down list contains timing field for two hours interval(8-10,10-12,12-2,2-4,4-6,6-8). I tried but it is not displaying any thing.
Thank you.
can you please show me what's you are doing?
ReplyDeleteNO it is showing username is available even if it exists in db.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIn the php code
ReplyDelete$username = mysql_real_escape_string($_POST['username']);
Should be :
$username = mysql_real_escape_string($_POST['user_name']);