Live Database Check using PHP Ajax

By | October 13, 2012

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" />

22 thoughts on “Live Database Check using PHP Ajax

    1. Eliza

      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.

      Reply
      1. prasad k Post author

        Sorry mate, it is not user_check.php, line number 32 in register.php. Thanks for your comment.

        Reply
  1. sam

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

    Reply
  2. FreedomSeeker

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

    Reply
    1. FreedomSeeker

      Sorry I meant more than one “input” but I wrote them as tags that’s why they get removed.

      Reply
  3. Ndahimana Donatien

    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.

    Reply
  4. abbas

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

    Reply
  5. Bhavik

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

    Reply
  6. Ravi

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

    Thank you.

    Reply
  7. will

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

    Reply

Leave a Reply

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

tawse-lexeme