Pages

Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Login form using php,Ajax and jquery with mysql

Login form using php,Ajax and jquery with mysql, Redirect one to another page using ajax with return result.

Create a index.php and add this code to it

<html> <body> <div class="wrap-input100 validate-input" data-validate = "Enter username"> <input class="input100" type="text" id="user" name="username" placeholder="Email"> <span class="focus-input100" data-placeholder="&#xf207;"></span> </div> <div class="wrap-input100 validate-input" data-validate="Enter password"> <input class="input100" type="password" id="pass" name="pass" placeholder="Password"> <span class="focus-input100" data-placeholder="&#xf191;"></span> </div> <div class="container-login100-form-btn"> <input class="input100" type="button" id="logBtn" name="logBtn" placeholder="Login" value="Login"> </div> <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script> $('#logBtn').click(function(event){ user = document.getElementById("user").value; password = document.getElementById("pass").value; $.ajax({ type:"POST", url:"Login.php", async: false, data: {user:user,password:password}, success: function(data){ alert(data); if(data=="admin"){ window.location="https://..Main/<file-name>.php"; } if(data=="user"){ window.location="https://.....<file-name>.php"; } } }); }); </script> </body> </html>
Create a Login.php

<?php

     $servername = "localhost";
     $username = "root";
     $password = "root";
     $dbname = "demo";

     $conn = new mysqli($servername, $username, $password, $dbname);

     $user = $_POST['user'];
     $pass = $_POST['password'];

     $sql = "SELECT * FROM users WHERE email='$user' AND pass='$pass'";

     $result = mysqli_query($conn, $sql);

     if (mysqli_num_rows($result) > 0) {
         $sql_1 = "SELECT * FROM users WHERE email='$user' AND pass='$pass' AND type='admin'";
          $result_1 = mysqli_query($conn, $sql_1);
         if (mysqli_num_rows($result_1) > 0){
             echo "admin";
             exit(0);
           }
            else{
            echo "user";
            exit(0);
         }

      } else {
         $msg = "username/password invalid";
         echo $msg;
      }

     mysqli_close($conn);
     ?>

Popular Posts