I need some help, the problem i am facing when add to cart is called it must show that item to cart. The logic i have so far its not showing that meaning when i add to cart it not displaying that item to be added.
What I have tried:
<pre><a href="" class="btn border">
<div class="card-footer d-flex justify-content-between bg-light border">
<a href="" class="btn btn-sm text-dark p-0">^__i class="fas fa-eye text-primary mr-1">View Detail</a>
<a href="" class="btn btn-sm text-dark p-0 add-to-cart-btn" data-id="1">^__i class="fas fa-shopping-cart text-primary mr-1">Add To Cart</a>
</div>
<pre>$(document).ready(function() {
$(document).on('click', '.add-to-cart-btn', function(e) {
e.preventDefault();
var itemId = $(this).data('id');
var isLoggedIn = false;
if (isLoggedIn) {
addToCart(itemId);
} else {
showLoginOrRegisterPrompt(itemId);
}
});
function addToCart(itemId) {
$.ajax({
url: 'add-to-cart.php',
method: 'POST',
data: { itemId: itemId },
dataType: 'json',
success: function(response) {
if (response.status === 'success') {
updateCartCount(response.cartItemCount);
console.log('Item added to cart.');
} else if (response.status === 'login_required') {
showLoginOrRegisterPrompt(itemId);
} else {
console.log('Error adding item to cart.');
}
},
error: function() {
console.log('An error occurred while adding item to cart.');
}
});
}
function showLoginOrRegisterPrompt(itemId) {
var modalContent = `
<div class="modal fade" id="loginRegisterModal" tabindex="-1" role="dialog" aria-labelledby="loginRegisterModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="loginRegisterModalLabel">Login or Register</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<!-- Login and register form here -->
<form id="loginForm" method="post" action="login.php">
<h6>Login</h6>
<div class="form-group">
<label for="loginEmail">Email</label>
<input type="email" class="form-control" id="loginEmail" name="loginEmail" required>
</div>
<div class="form-group">
<label for="loginPassword">Password</label>
<input type="password" class="form-control" id="loginPassword" name="loginPassword" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
<hr>
<form id="registerForm" method="post" action="register.php">
<h6>Register</h6>
<div class="form-group">
<label for="registerName">Name</label>
<input type="text" class="form-control" id="registerName" name="registerName" required>
</div>
<div class="form-group">
<label for="registerEmail">Email</label>
<input type="email" class="form-control" id="registerEmail" name="registerEmail" required>
</div>
<div class="form-group">
<label for="registerPassword">Password</label>
<input type="password" class="form-control" id="registerPassword" name="registerPassword" required>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</div>
</div>
</div>
`;
$('body').append(modalContent);
$('#loginRegisterModal').modal('show');
$('#loginRegisterModal').on('hidden.bs.modal', function() {
$(this).remove();
});
}
});
<pre><?php
session_start();
$dbHost = 'localhost';
$dbName = 'ecommerce_store';
$dbUser = 'root';
$dbPass = '';
try {
$pdo = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
exit;
}
if (isset($_POST['email']) && isset($_POST['password'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = :email");
$stmt->execute(['email' => $email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user'] = $user;
$_SESSION['logged_in'] = true;
header("Location: shopping.php");
exit();
} else {
echo "failure";
}
}
?>
<pre><?php
session_start();
require_once 'dbconn.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$itemId = $_POST['itemId'];
if (isset($_SESSION['id'])) {
$userId = $_SESSION['id'];
$query = "INSERT INTO cart (id, product_name) VALUES (:id, :product_name)";
$stmt = $db->prepare($query);
$stmt->bindParam(':id', $userId);
$stmt->bindParam(':product_name', $product_name);
$stmt->execute();
$cartItemCount = getCartItemCount($db, $userId);
$response = array(
'status' => 'success',
'cartItemCount' => $cartItemCount
);
} else {
$response = array('status' => 'login_required');
}
header('Content-Type: application/json');
echo json_encode($response);
}
function getCartItemCount($db, $userId) {
$query = "SELECT COUNT(*) FROM cart WHERE id = :id";
$stmt = $db->prepare($query);
$stmt->bindParam(':id', $userId);
$stmt->execute();
return $stmt->fetchColumn();
}
?>