article

Saturday, November 14, 2020

Live Username Available using PHP Mysqli and Jquery Ajax


Live Username Available using PHP Mysqli and Jquery Ajax

check username before inserting to database
check_username_availabilit.php
//check_username_availabilit.php
<html>  
 <head>  
  <title>Live Username Available using PHP Mysqli and Jquery Ajax</title>  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
  <style>  
  body  
  {  
   margin:0;  
   padding:0;  
   background-color:#f1f1f1;  
  }  
  .box  
  {  
   width:800px;  
   border:1px solid #ccc;  
   background-color:#fff;  
   border-radius:5px;
   margin-top:36px;  
  }  
  </style>  
 </head>  
 <body>  
<?php   
include"dbcon.php";
$msg = "";
if(isset($_POST["register"]))
{
 $username = $_POST["username"];
 $txtemail = $_POST["txtemail"];
 $txtpass = $_POST["txtpass"];
 $sql = "INSERT INTO users(username, email, password) VALUES ('$username', '$txtemail', '$txtpass')"; 
 $conn->query($sql); 
 $msg = "Succesfully Register";
}
?> 
  <div class="container box">  
   <div class="form-group">  
    <h3 align="center">Live Username Available using PHP Mysqli and Jquery Ajax</h3><br />  
	<?php echo $msg; ?>
    <form class="form-signin" action="" method="post">
	<label>Enter Username</label>  
    <input type="text" name="username" id="username" class="form-control" />
    <span id="availability"></span>
    <br />
	<label>Enter Email</label>  
    <input type="text" name="txtemail" id="txtemail" class="form-control" />
	<label>Enter Password</label>  
    <input type="text" name="txtpass" id="txtpass" class="form-control" />
	<br />
    <button type="submit" name="register" class="btn btn-info" id="register" disabled>Register</button>
	</form>
    <br />
   </div>  
   <br />  
   <br />  
  </div>  
 </body>  
</html>  
<script>  
 $(document).ready(function(){  
   $('#username').blur(function(){

     var username = $(this).val();

     $.ajax({
      url:'check_username.php',
      method:"POST",
      data:{user_name:username},
      success:function(data)
      { //alert(data)
       if(data == '0')
       {
        $('#availability').html('<span class="text-danger">Username not available</span>');
        $('#register').attr("disabled", false);
       }
       else
       {
        $('#availability').html('<span class="text-success">Username Available</span>');
        $('#register').attr("disabled", true);
       }
      }
     })

  });
 });  
</script>
check_username.php
//check_username.php
<?php   
include"dbcon.php";
if(isset($_POST["user_name"]))
{
 $username = $_POST["user_name"];
 $query = "SELECT * FROM users WHERE username = '".$username."'";
 $result = mysqli_query($conn, $query);
 echo mysqli_num_rows($result);
}
?>
dbcon.php
//dbcon.php
<?php
$conn = new mysqli('localhost','root','','testingdb');
if ($conn->connect_error) {
    die('Error : ('. $conn->connect_errno .') '. $conn->connect_error);
}
?>

Related Post