Most of the websites are using username as primary key, then username should not be repeated. In this article i am going to explain how to live check the username in database using ajax and php.
There are two files
- register.php
- user_check.php
Database
username is the primary key.
Register.php
[code type=html]
<html>
<head>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”></script>
<script type=”text/javascript”>
$(document).ready(function(){
$(“#name”).keyup(function() {
var name = $(‘#name’).val();
if(name==””)
{
$(“#disp”).html(“”);
}
else
{
$.ajax({
type: “POST”,
url: “user_check.php”,
data: “name=”+ name ,
success: function(html){
$(“#disp”).html(html);
}
});
return false;
}
});
});
</script>
</head>
<body>
<fieldset style=”width:250px;”>
<h3>Username Check using Ajax</h3>
<form method=”post”>
Username: <input type=”text” name=”name” id=”name” /><br /><br />
<div id=”disp”></div><br />
<input type=”submit” name=”submit” />
</form>
</fieldset>
</body>
</html>
[/code]
user_check.php
[code type=php]
<?php
include(‘db.php’);
if(isset($_POST[‘name’]))
{
$name=mysql_real_escape_string($_POST[‘name’]);
$query=mysql_query(“select * from user where username=’$name'”);
$row=mysql_num_rows($query);
if($row==0)
{
echo “<span style=’color:green;’>Available</span>”;
}
else
{
echo “<span style=’color:red;’>Already exist</span>”;
}
}
?>
[/code]
Result:
Username “prasad” is already present in table. If we give username as “prasa”, then this script will allow user to register.
If we give username that already exist in database then “Already exist” message will be displayed.
Check Username in Database using PHP Ajax with Check Availability Button
In some registration page, a button will be available. When we click that button, then it will validate our username with database values.
To make this kind of validate, just do some changes in the above script.
Register.php
Replace Line 6 By this
[code type=javascript]
$(“#check”).click(function() {
[/code]
Register.php
Replace Line 32 By this
[code type=html]
[/code]
so thanks ful to you.. i at last run my project
In the user_check.php ther is no line 32.
Would you please explain. Is there more to this than the 2 files.
Does it need a database which I haven’t got? I replaced Line 6 with this $(“#check”).click(function() in Register.php but the check didn’t show.
Sorry mate, it is not user_check.php, line number 32 in register.php. Thanks for your comment.
nice man..
very good..
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\user_check.php on line 7
Available
Thanks for the amazing code.
I have a question say I have more than one and I want to check everyone if them and have “available” or something like that displayed for every what should I do??
Sorry I meant more than one “input” but I wrote them as tags that’s why they get removed.
nice…
filter form input using specialchars()
$name=htmlspecialchars($_POST[‘name’]);
thanks its work and very much simple
it’s also possible without primary key (username)..
Its great, Thanks
How unequalled is this website?, I will never forget the level of web design I were, the level I’m & the level I will be ‘coz of this website !!!. Thanks In Advance.
itz very good……..
awsm work
well thank you so much
you have written simple code i got it too quickly
again thank you so much
I have a silly doubt here – what if user submits the form even if the username is unavailable?
Hi, it’s a very simple way of live check function. We have to do based on our options.
I want to use onChange insted of keyup but it not work..
any suggestion for me.
Thank you.
Just use .change() jquery function.
Thank you . You saved my time….
Hurrah! Finally I got a blog from where I know how to actually take helpful facts concerning my study and knowledge.
I am truly thankful to the holder of this website who has shared
this enormous post at at this time.
Pretty! This has been a really wonderful
article. Thank you for providing this information.