PHP - Login Form With jQuery

Submitted by: 
Language: 
Visitors have accessed this post 561 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Login Form With jQuery</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3">
  19. <h6>Default username: admin</h6>
  20. <h6>Default password: admin</h6>
  21. </div>
  22. <div class="col-md-6">
  23. <form method="POST">
  24. <div class="form-group">
  25. <label>Username</label>
  26. <input type="text" id="username" class="form-control"/>
  27. </div>
  28. <div class="form-group">
  29. <label>Password</label>
  30. <input type="password" id="password" class="form-control"/>
  31. </div>
  32.  
  33. <center><button type="button" id="login" class="btn btn-primary"><span class="glyphicon glyphicon-log-in"></span> Login</button></center>
  34. <br />
  35. <div id="result"></div>
  36. </form>
  37. </div>
  38. </div>
  39. <script src="js/jquery-3.2.1.min.js"></script>
  40. <script src="js/script.js"></script>
  41. </body>
  42. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.