Click here to Skip to main content
15,799,363 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team

I am having an issue with my signin form its not redirecting to the login and from the login. When signing using correct details its not, but remain on the login page instead of shopping page. What am missing here?

What I have tried:

// Database connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecommerce_store";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception

    if (isset($_POST['email']) && isset($_POST['password'])) {
        $email = $_POST['email'];
        $password = $_POST['password'];
        // Check if user exists
        $stmt = $conn->prepare("SELECT * FROM users WHERE email = :email");
        $stmt->bindParam(':email', $email);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($user) {
            // Verify password
            if (password_verify($password, $user['password'])) {
                // Store user data in session
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_name'] = $user['name'];
                // Redirect to dashboard or shopping page
                header("Location: shopping.php");
            } else {
                $_SESSION['login_error'] = 'Invalid email or password.';
                header("Location: login.php");
        } else {
            $_SESSION['login_error'] = 'Invalid email or password.';
            header("Location: login.php");
} catch(PDOException $e) {
    error_log("Error: " . $e->getMessage());
    $_SESSION['login_error'] = 'Error logging in. Please try again later.';
    header("Location: login.php");

// js file
<pre>$(document).ready(function() {
  // Handle click event on the cart badge
  $('#cart-badge-btn').click(function(e) {
    // Show the login modal form

  // Handle form submission in the login modal
  $('#loginForm').submit(function(e) {

    // Get the form data
    var formData = $(this).serialize();

    // Submit the login form using AJAX
      type: 'POST',
      url: 'login.php', // Submit the form to login.php
      data: formData,
      success: function(response) {
        // Handle the success response
        if (response === 'success') {
          // Redirect the user to shopping.php
          window.location.href = 'shopping.php';
        } else {
          // Handle login error
          $('#login-error-message').text('Invalid credentials. Please try again.');
      error: function(xhr, status, error) {
        // Handle the error
        alert('An error occurred while logging in.');


// html code
<pre><!--Modal form popup starts-->
<div class="modal" id="loginModal">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- Modal header, body, and footer -->
      <div class="modal-header">
        <h5 class="modal-title">Login</h5>
        <button type="button" class="close" data-dismiss="modal">×</button>
      <div class="modal-body">
        <!-- Login form -->
        <form id="loginForm" method="POST" action="login.php">
          <!-- Add your login form fields here (e.g., username and password) -->
          <div class="form-group">
            <label for="username">Username</label>
            <input type="text" name="username" id="username" class="form-control" required>
          <div class="form-group">
            <label for="password">Password</label>
            <input type="password" name="password" id="password" class="form-control" required>
          <button type="submit" class="btn btn-primary">Login</button>
Updated 5-Jul-23 7:11am
Andre Oosthuizen 7-Jul-23 4:53am    
In your html code, where is the '#cart-badge-btn'?
Gcobani Mkontwana 7-Jul-23 6:59am    
@Andre Oosthuizen,


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900