PHP - Login Form With jQuery

In this tutorial we will create a Login Form With jQuery using PHP. This code will login a user account without refreshing the web page. The code use AJAX script to submit a binded form input to the database server to check whether the user account exist in the table. This is a user-friendly kind of program feel free to modify it and use it as your own. We will be using jQuery to simplify and the client-side scripting of HTML. The use of jQuery is to provide an easy way to use JavaScript and Ajax API that works on a lot of different type of browsers. .

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_login, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2.         $conn = mysqli_connect("localhost", "root", "", "db_login");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database!");
  6.         }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Login Form With jQuery</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-3">
  18.                         <h6>Default username: admin</h6>
  19.                         <h6>Default password: admin</h6>
  20.                 </div>
  21.                 <div class="col-md-6">
  22.                         <form method="POST">
  23.                                 <div class="form-group">
  24.                                         <label>Username</label>
  25.                                         <input type="text" id="username" class="form-control"/>
  26.                                 </div>
  27.                                 <div class="form-group">
  28.                                         <label>Password</label>
  29.                                         <input type="password" id="password" class="form-control"/>
  30.                                 </div>
  31.                
  32.                                 <center><button type="button" id="login" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  33.                                 <br />
  34.                                 <div id="result"></div>
  35.                         </form>
  36.                 </div>
  37.         </div>
  38. <script src="js/jquery-3.2.1.min.js"></script> 
  39. <script src="js/script.js"></script>
  40. </body>
  41. </html>

Creating the Main Function

This code contains the main function of the application. This code will login the user account when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. login.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         $username = $_POST['username'];
  5.         $password = $_POST['password'];
  6.  
  7.         $query = mysqli_query($conn, "SELECT * FROM `user` WHERE `username` = '$username' AND `password` = '$password'") or die(mysqli_error());
  8.         $row = mysqli_num_rows($query);
  9.        
  10.         if($row > 0){
  11.                 echo "success";
  12.         }else{
  13.                 echo "error";
  14.         }
  15. ?>
script.js Note: Make sure you save this file inside the js directory to make this work.
  1. $(document).ready(function(){
  2.         $('#login').on('click', function(){
  3.                 var username = $('#username').val();
  4.                 var password = $('#password').val();
  5.                        
  6.                 if($('#username').val() == "" || $('#password').val() == ""){                  
  7.                         var warning = $("<div class='alert alert-danger'>Please complete the required field!</div>");
  8.                         $('#result').append(warning);
  9.                         setTimeout(function(){
  10.                                 warning.fadeOut(1000);
  11.                         }, 2000);
  12.                 }else{
  13.                         $.ajax({
  14.                                 type: 'POST',
  15.                                 url: 'login.php',
  16.                                 data: {
  17.                                         username: username,
  18.                                         password: password
  19.                                 },
  20.                                 success: function(data){
  21.                                         if(data === "success"){
  22.                                                 var info = $("<div class='alert alert-success'>Login succesfully</div>");
  23.                                                 $('#result').append(info);
  24.                                                 setTimeout(function(){
  25.                                                         info.fadeOut(1000);
  26.                                                 }, 2000);
  27.                                         }else if(data === "error"){
  28.                                                 var warning = $("<div class='alert alert-danger'>Invalid username or password</div>");
  29.                                                 $('#result').append(warning);
  30.                                                 setTimeout(function(){
  31.                                                         warning.fadeOut(1000);
  32.                                                 }, 2000);
  33.                                         }
  34.                                 }
  35.                         });
  36.                 }
  37.         });
  38. });
There you have it we successfully created Login Form With jQuery using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment