-->

Pages

Tuesday 7 March 2017

Check User Name Availability using AJAX,PHP and MYSQL !

Whenever we create a new emailID in Gmail or create some new user on some other website, we often get an error message if the username or the emailID we are selecting is not available, that this username or the entered emailID is not available, please try some other name.

In this article of mine, I am going to show how you can do this using AJAX and PHP and my database is MySQL.

My Connection File: connect.php

<?php

$user="root";
$pass="root";
$server = "localhost";
$con=mysql_connect($server,$user,$pass);
if(mysql_select_db('MyDB'))
{
    //echo 'database selected';
}

if(!$con)
{
    echo 'No connection found';
}
 else {
   // echo 'Connection established';
 }
?>

index.php:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>UserName Check</title>
        <script>
function checkMail(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
     //AJAX code started
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                
            }
            
        };
//getMail.php is the file where I am checking the email Address
        xmlhttp.open("GET","getMail.php?q="+str,true);
        xmlhttp.send();
       
    }
}


</script>
    </head>
    <body>
//insert.php is the file where the insert code is written
        <form action="insert.php" method="POST">

        EMAIL: <input type="email" name="email" id="email" onchange="checkMail(this.value)"/><br/><br/>
            <input type="submit" value="SUBMIT"/>
            
        </form>
        <div id="txtHint"></div>
    </body>
</html>

getMail.php

<?php
$q = $_GET['q'];//getting the query variable
include './connect.php';
$sql="SELECT EMAIL FROM regr WHERE EMAIL = '$q'";
$result = mysql_query($sql);
if($res=  mysql_fetch_assoc($result))
{
    ?>
//if id is existing this response will be sent
<p style="color:red">* EmailID is Already Existing !</p>

<?php
}
 else {
       ?>
//if id is new this response will be generated
<p style="color:green">* Go Ahead ! Your EmailID is Unique!</p>
<?php
 }

?>

insert.php   
  
<?php     
error_reporting(0);
include './connect.php';
$email=  trim($_REQUEST['email']);//trim function is used to trim off the whitespaces
if(isset ($email))//isset() is used to check that the variable is set and it is not null.
{
    
        $query="insert into regr(EMAIL) values('$email')";
mysql_query($query);
       
if(mysql_affected_rows()>0)
            {
            header("Location:index.php");//header function is used to redirect after insertion is done
            }
}
?>

Screenshots:
WHEN I ENTERED THE MAIL ID FOR THE VERY FIRST TIME

WHEN I ENTERED THE ALREADY EXISTING ID

That's it in this coding article of mine, try to implement it and if you guys have some problem in doing so, feel free to ask me!

No comments:

Post a Comment

Thanks for Your Time!