Live Database Check using PHP Ajax

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<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>

 

user_check.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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>";
}
}
?>

 

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

1
$("#check").click(function() {

 

Register.php

Replace Line 32 By this

1
2
3
4
<input id="name" type="text" name="name" />

<input id="check" type="submit" name="check" value="check" />

About prasad k

Prasad Kuppusamy has written 81 post in this blog.

i m php developer, web designer.

22 Responses to Live Database Check using PHP Ajax

  1. abhishek says:

    so thanks ful to you.. i at last run my project

    • Eliza says:

      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.

  2. sam says:

    Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\user_check.php on line 7
    Available

  3. FreedomSeeker says:

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

  4. vir0e5 says:

    nice…

    filter form input using specialchars()

    $name=htmlspecialchars($_POST['name']);

  5. raza says:

    thanks its work and very much simple

  6. Hiren says:

    it’s also possible without primary key (username)..

  7. sanjiv says:

    Its great, Thanks

  8. Ndahimana Donatien says:

    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.

  9. sagar says:

    itz very good……..

    awsm work

  10. abbas says:

    well thank you so much
    you have written simple code i got it too quickly
    again thank you so much

  11. Bhavik says:

    I have a silly doubt here – what if user submits the form even if the username is unavailable?

  12. Ravi says:

    I want to use onChange insted of keyup but it not work..
    any suggestion for me.

    Thank you.

  13. Nikhil says:

    Thank you . You saved my time….

  14. Hurrah! Finally I got a blog from where I know how to actually take helpful facts concerning my study and knowledge.

  15. will says:

    I am truly thankful to the holder of this website who has shared
    this enormous post at at this time.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

exsolve-replow