Check Email Availability using Ajax php
Hi friends, in this post we will discuss how to Check Email Availability using Ajax php. It is something like, user earlier registered with us or a new user. It will provide you information live and user don’t want to reload the entire form in case of email registered earlier.

In earlier post we had discussed about #1045 – Access denied for user ‘root’@’localhost’ (using password: NO), Foreign key in phpmyadmin, Excerpts in Twenty Twelve Theme and Editor Feature is not Showing in WordPress.
Then design the page, i am just taking one textbox for email id, you can take as many controls as you want. In this on key release by user code made ajax call, we will be using jQuery $.post method, it is the easiest way to load data from the server using a HTTP POST request. On check with server it will display availability of username.
First we have to create users table, then insert some dummy data into that:
Database table Creation Script with Dummy data:
2 | CREATE TABLE IF NOT EXISTS `users` ( `id` int (11) NOT NULL AUTO_INCREMENT, `username` varchar (60) NOT NULL , PRIMARY KEY (`id`) ) |
5 | INSERT INTO `hightechnology`.`users` (`id`, `username`) VALUES ( NULL , 'info@hightechnology.in' ), ( NULL , 'Test@hightechnology.in' ); |
Check Email Availability using Ajax php Design:
04 | < meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" /> |
05 | < title >Check Email Availability using Ajax php</ title > |
06 | < script type = "text/javascript" src = "js/jquery-1.9.0.min.js" ></ script > |
07 | < script type = "text/javascript" > |
08 | $(document).ready(function() { |
09 | $("#username").keyup(function (e) { |
11 | //removes spaces from username |
12 | $(this).val($(this).val().replace(/\s/g, '')); |
14 | var username = $(this).val(); |
15 | if(username.length < 4 ){$("#user-result").html('');return;} |
17 | if(username.length >= 4){ |
18 | $("#user-result").html('< img src = "img/ajax-loader.gif" />'); |
19 | $.post('check_email.php', {'username':username}, function(data) { |
20 | $("#user-result").html(data); |
26 | < style type = "text/css" > |
45 | border: 1px solid #E9E9E9; |
52 | < h1 >Check Email Availability using Ajax php</ h1 > |
53 | < div id = "registration-form" > |
54 | < label for = "username" >Enter Username : |
55 | < input name = "username" type = "text" id = "username" > |
56 | < span id = "user-result" ></ span > |
59 | < br />< br />< br />< br /> |
62 | Change GridView Row Color Based on Value of Row</ a > | Hosting partner |
Check Email Availability using Ajax php Code:
05 | $db_name = 'hightechnology' ; |
06 | $db_host = 'localhost' ; |
11 | if (isset( $_POST [ "username" ])) |
14 | if (!isset( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ]) AND strtolower ( $_SERVER [ 'HTTP_X_REQUESTED_WITH' ]) != 'xmlhttprequest' ) { |
19 | $connecDB = mysqli_connect( $db_host , $db_username , $db_password , $db_name ) or die ( 'could not connect to database' ); |
22 | $username = strtolower (trim( $_POST [ "username" ])); |
24 | $username = filter_var( $username , FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH); |
26 | $results = mysqli_query( $connecDB , "SELECT id FROM users WHERE username='$username'" ); |
29 | $username_exist = mysqli_num_rows( $results ); |
33 | die ( '<img src="img/not-available.png" />' ); |
35 | die ( '<img src="img/available.png" />' ); |
39 | mysqli_close( $connecDB ); |
