When We try to Register for any website and On the Username Field We just type your desired Username and within Seconds Without Refreshing the Page the system Checks if the username Is Available or Not so today's my tutorial is based on the same functionality.
we have Following 4 Files in our Directory And Each file is going to perform its own function.
Following Function Will Be Performed When Our Username is being Checked
So it will show just an text field and and a span is there with an id "username_status" this span is for showing the status message.
Now lets write the user.js code. The jquery code is written below. This code will fetch the data from the index.php and send it to username_check.php and then will print the result to the page.
Now we here comes the username_check.php file which will perform the main functionality of this application.
we have Following 4 Files in our Directory And Each file is going to perform its own function.
- Index.php
- user.js
- db.php
- username_check.php
Following Function Will Be Performed When Our Username is being Checked
Download
- Username is fetched from index.php and send to the server for checking.
- if the username is taken then an error message will be shown
- else there will be a available message.
so lets start
First we have To create a index file. I am not using any stylesheet for this. its just an simple form without any formatting or styling.
so here is the code for index.php file
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title> Jquery Username Check Tutorial</title> </head> <body> <input id="username" type="text" /><span id="username_status"></span> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/users.js"></script> </body> </html>
So it will show just an text field and and a span is there with an id "username_status" this span is for showing the status message.
Now lets write the user.js code. The jquery code is written below. This code will fetch the data from the index.php and send it to username_check.php and then will print the result to the page.
$('#username').keyup(function(){ var username = $(this).val(); $('#username_status').text('searching....'); if(username != '') { $.post('username.php',{ username:username },function(data){ $('#username_status').text(data); }); } else { $('#username_status').text('Nothing'); } });
Now we here comes the username_check.php file which will perform the main functionality of this application.
<?php if(isset($_POST['username'])) { $username = mysql_real_escape_string($_POST['username']); echo $username; if(!empty($username)) { $username_query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"); $username_result = mysql_result($username_query,0); if($username_result == 0) { echo 'Username available!'; } else ($username_result ==1) { echo 'Sorry, That username is Taken!'; } } ?>
And your are Done This will do everything you just have to make a database connection and this will start its working.
Comments
Post a Comment
your Comment is sent for moderation, Thankyou