Click here to Skip to main content
16,020,565 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:

PHP
<?php
session_start();
// 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
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_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);
        $stmt->execute();
        $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");
                exit();
            } else {
                $_SESSION['login_error'] = 'Invalid email or password.';
                header("Location: login.php");
                exit();
            }
        } else {
            $_SESSION['login_error'] = 'Invalid email or password.';
            header("Location: login.php");
            exit();
        }
    }
} catch(PDOException $e) {
    error_log("Error: " . $e->getMessage());
    $_SESSION['login_error'] = 'Error logging in. Please try again later.';
    header("Location: login.php");
    exit();
}
?>


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

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

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

    // Submit the login form using AJAX
    $.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.');
        console.log(xhr.responseText);
      }
    });
  });

  
});


// html code
HTML
<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>
      <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>
          <div class="form-group">
            <label for="password">Password</label>
            <input type="password" name="password" id="password" class="form-control" required>
          </div>
          <button type="submit" class="btn btn-primary">Login</button>
        </form>
      </div>
    </div>
  </div>
</div>
Posted
Updated 5-Jul-23 6:11am
v2
Comments
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,

0

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