Ajax Login Form with Bootstrap, jQuery, and PHP MySQL

In this tutorial, we are going to explain how to create Ajax Login Form with Bootstrap, jQuery, and PHP MySQL. For my Registration tutorial post using Ajax and jQuery, this is my continuation tutorial that I am going to create a simple yet useful login form with Bootstrap. The users type their email and password to log in and all the process will be done without web page refresh. Kindly download the source code below, and let’s start to code. Read also: PHP Forms Submit without Page Refresh using jQuery and Ajax Database Configuration This file helps us to modify the database name.
  1. <?php
  2.  
  3.         $database_host = "localhost";
  4.         $database_user = "root";
  5.         $database_password = "";
  6.         $database_name = "login_ajax";
  7.        
  8.         try{
  9.                
  10.                 $db_con = new PDO("mysql:host={$database_host};dbname={$database_name}",$database_user,$database_password);
  11.                 $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.         }
  13.         catch(PDOException $display_message){
  14.                 echo $display_message->getMessage();
  15.         }
  16.  
  17. ?>
Bootstrap Link Kindly copy and paste this two files to load Bootstrap to your HEAD tag of your web page.
  1. <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  2. <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
  3. <link href="style.css" rel="stylesheet" type="text/css" media="screen">
Markup Login Form This source code below, where the user types their email and password to log in.
  1. <div class="signin-form">
  2.         <div class="container">
  3.        <form class="form-signin" method="post" id="login-form">
  4.         <h2 class="form-signin-heading" style="text-align:center;">Log In Form</h2><hr />
  5.        
  6.         <div id="error">
  7.         <!-- error will be shown here ! -->
  8.         </div>
  9.        
  10.         <div class="form-group">
  11.         <input type="email" class="form-control" placeholder="Email address . . . . ." name="user_email" id="user_email" autofocus="autofocus" />
  12.         <span id="check-e"></span>
  13.         </div>
  14.        
  15.         <div class="form-group">
  16.         <input type="password" class="form-control" placeholder="Password . . . . . " name="password" id="password" />
  17.         </div>
  18.                
  19.         <hr />
  20.        
  21.         <div class="form-group">
  22.             <button type="submit" class="btn btn-primary" name="btn-login" id="btn-login">
  23.                 <span class="glyphicon glyphicon-log-in"></span> Sign In
  24.                         </button>
  25.         </div>  
  26.       </form>
  27.     </div>
  28. </div>
Login PHP Query It contains PHP source code, this query verifies the email and password of the user. If the login information is a success it will display the ok message or if failed the information given from the user it will display the wrong details alert or message.
  1. <?php
  2.         session_start();
  3.         require_once 'dbconfig.php';
  4.  
  5.         if(isset($_POST['btn-login']))
  6.         {
  7.                 $user_email = trim($_POST['user_email']);
  8.                 $user_password = trim($_POST['password']);
  9.                
  10.                 $password = ($user_password);
  11.                
  12.                 try
  13.                 {      
  14.                         $statement = $db_con->prepare("SELECT * FROM tbl_users WHERE user_email=:email");
  15.                         $statement->execute(array(":email"=>$user_email));
  16.                         $row = $statement->fetch(PDO::FETCH_ASSOC);
  17.                         $count = $statement->rowCount();
  18.                        
  19.                         if($row['user_password']==$password){
  20.                                
  21.                                 echo "Successfully Login"; // log in
  22.                                 $_SESSION['user_session'] = $row['user_id'];
  23.                         }
  24.                         else{
  25.                                 echo "Email or password does not exist."; // wrong details
  26.                         }
  27.                 }
  28.                 catch(PDOException $e){
  29.                         echo $e->getMessage();
  30.                 }
  31.         }
  32. ?>

Output

Email and password validation. Result Wrong information in Login Form. Result If the user successfully login before jump to the home page. Result This is the home page. Result Read also: PHP Forms Submit without Page Refresh using jQuery and Ajax That's it, kindly click the "Download Code" button below for the full source code. And try it to customize your Login Form. Enjoy coding. Hope that this simple yet useful tutorials that I created may help you to your future projects. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment