Login using PHP and JavaScript from SQL database

0 votes

I have developed a drop-down login panel using Bootstrap 3.3.7. Where the user inputs the username and password used during registration. What I want is, If login is successful I want the user to be redirected to another page called profile.php else if login is not successful an error message will appear on the login panel footer.

The HTML code for the panel:

<li class="nav-link dropdown">
  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><span class="glyphicon glyphicon-user"></span>Login</a>
  <div class="dropdown-menu" aria-labelledby="navbarDropdown">
    <div style="width:300px;">
      <div class="panel-heading text-center" style="color:black;">LOGIN</div>
      <div class="panel-heading">
        <label for="email">username</label>
        <input type="text" class="form-control" id="username">
        <label for="email">Password</label>
        <input type="password" class="form-control" id="password">
        <br>
        <a href="#" style="color:blue; list-style:none;">Forgotten password?</a><input type="submit" class="btn btn-success" style="float:right;" id="login" value="login">
      </div>
      <div class="panel-footer" id="login_msg"></div>
    </div>
  </div>
</li>

The  javascript login code:

$("#login").click(function(event) {
  event.preventDefault();
  var u_name = $("#username").val();
  var pass = $("#password").val();
  $.ajax({
    url: "login.php",
    method: "POST",
    data: {
      userLogin: 1,
      userUname: u_name,
      userPassword: pass
    },
    success: function(data) {
      if (data == "truejsdhfkh") {
        window.location.href = "profile.php";
      } else {
        $("#login_msg").html(data);
      }
    }
  })
})

The php login code:

<?php
// connect to the database
include "db.php";
//session start
session_start();
//login page
if(isset($_POST['userLogin'])){

    $uname = mysqli_real_escape_string($con, $_POST['userUname']);
    $passwrd = mysqli_real_escape_string($con, $_POST['userPassword']);


    if(empty($uname) || empty($passwrd)){
        echo "
        <div class='alert alert-danger'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><b>Please fill in all fields!</b>
        </div>
    ";
    exit();
    }else{
        $passwrd = md5($passwrd);
        $sql = "SELECT * FROM user_info WHERE username = '$u_name' AND passwrd = '$password'";
        $run_query = mysqli_query($con,$sql); 
        $count = mysqli_num_rows($run_query);
        if($count == 1){
            $row = mysqli_fetch_array($run_query);
            $_SESSION["uid"] = $row["user_id"];
            $_SESSION["uname"] = $row["username"];
            echo "truejsdhfkh";
    }
    }

}
?>

When I click on the login button nothing happened. Can someone please guide me in doing this?

Jun 11, 2022 in PHP by narikkadan
• 63,680 points
3,771 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
0 answers

How can I store and retrieve images from a MySQL database using PHP?

How can I insert an image in ...READ MORE

Jun 14, 2022 in PHP by narikkadan
• 63,680 points
473 views
0 votes
1 answer

How to retrieve or obtain data from the MySQL database using PHP?

Hello kartik,  Actually there are many functions that  ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
3,327 views
0 votes
1 answer

How to connect to SQL Server database from JavaScript in the browser?

Hello @kartik, You shouldn´t use client javascript to ...READ MORE

answered Jul 21, 2020 in PHP by Niroj
• 82,880 points
23,919 views
0 votes
0 answers

How to display image from database using PHP?

I want to display an image from ...READ MORE

May 11, 2022 in PHP by Kichu
• 19,050 points
772 views
0 votes
0 answers

How to extract img src, title and alt from html using php?

I am working on a page where ...READ MORE

Jun 3, 2022 in PHP by Kichu
• 19,050 points
1,026 views
0 votes
0 answers

How to download file from database/folder using php

I want to know how to create ...READ MORE

Jun 27, 2022 in PHP by narikkadan
• 63,680 points
7,199 views
0 votes
1 answer
0 votes
0 answers

Anyone can help me out to understand the semantic of (document.getElementBYId("demo").innerHTML="Hello") ?

Hello guys, Can Someone helps me to find ...READ MORE

Jan 17, 2020 in Web Development by anonymous
• 37,520 points
901 views
+1 vote
1 answer

What is the relationship between angularjs Scope with controller/view?

Let us consider the below block: <div ng-controller="emp"> ...READ MORE

answered Jan 20, 2020 in Web Development by Niroj
• 82,880 points

edited Jan 21, 2020 by Niroj 1,099 views
0 votes
1 answer

What is data binding in AngularJS?

Data binding is synchronization of data between the ...READ MORE

answered Jan 23, 2020 in Web Development by Niroj
• 82,880 points
1,075 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP