I have a problem. I have an item from the cart but now create a function to remove these item from the cart. Somehow, the function does not seem to see it as being defined. How do I fix this? I did dome debugging and noticed this js error.
Uncaught ReferenceError: removeItemFromCart is not defined
onclick https://splashonline.co.za/home.php:1
What I have tried:
$(document).ready(function() {
$(".add-to-cart-btn").click(function(e) {
e.preventDefault();
var productId = $(this).data("id");
console.log("Product ID:", productId);
var productName = $(this).data("product-name");
var productImage = $(this).data("product-image");
var quantity = parseInt($("#quantity-input").val());
var productImage = "img/" + productId + ".jpg";
var productPrice = parseFloat($("#product-price-" + productId).text().replace("R", ""));
var totalPrice = productPrice * quantity;
var cartItems = JSON.parse(sessionStorage.getItem("cartItems")) || [];
cartItems.push({
productId: productId,
quantity: quantity,
productImage: productImage,
productName: productName
});
sessionStorage.setItem("cartItems", JSON.stringify(cartItems));
$.ajax({
type: "POST",
url: "add-to-cart.php",
data: { productId: productId },
success: function(response) {
alert(response);
updateCartBadge();
getCartItem();
},
error: function(xhr, status, error) {
alert("An error occurred while adding the item to the cart.");
console.log(xhr.responseText);
}
});
});
function updateCartBadge(userId) {
$.ajax({
type: "GET",
url: "getCartItemCount.php",
data: { userId: userId},
success: function (response) {
console.log('Cart Item Count:', response);
$("#cart-badge").text(response);
},
error: function (xhr, status, error) {
alert("An error occurred while retrieving the cart item count.");
console.log(xhr.responseText);
}
});
}
function updateCartItems(cartData) {
var cartItems = cartData.cartItems;
var cartContainer = $("#cart-items");
cartContainer.empty();
cartItems.forEach(function (item) {
var totalPrice = item.totalPrice || 0;
var cartItemHtml = `
<tr>
<td>${item.product_name}</td>
<td><img src="${item.product_image}"
alt="${item.product_name}" class="img-thumbnail"></td>
<td>R${totalPrice.toFixed(2)}</td>
<td>${item.quantity}</td>
<td><button class="btn btn-danger btn-sm"
data-productId="${item.product_id}"
onclick="removeItemFromCart(${item.product_id})">Remove
</button></td>
</tr>
`;
cartContainer.append(cartItemHtml);
});
var totalPriceContainer = $("#total-price");
totalPriceContainer.text("Total Price: R" + cartData.totalPrice.toFixed(2));
}
$("#cartModal").on("click", ".remove-cart-item", function () {
var productId = $(this).data("productId");
removeItemFromCart(productId);
});
function removeItemFromCart(productId) {
$.ajax({
type: "POST",
url: "removeCartItem.php",
data: { productId: productId },
dataType: "json",
success: function (response) {
if (response.success) {
getCartItemsForModal();
updateCartItems();
} else {
alert(response.message);
}
},
error: function (xhr, status, error) {
alert("An error occurred while removing the item from the cart.");
console.log(xhr.responseText);
},
});
}
$("#cartModal").on("show.bs.modal", function () {
$.ajax({
type: "GET",
url: "getCartData.php",
dataType: "json",
success: function (cartData) {
console.log("Response from the server:", cartData);
updateCartItems(cartData);
updateCartBadge();
},
error: function (xhr, status, error) {
alert("An error occurred while fetching cart item details");
console.log(xhr.responseText);
}
});
});
function getCartItemsForModal() {
$.ajax({
type: "GET",
url: "getCartItem.php",
dataType: "html",
success: function (response) {
$("#cart-items").html(response);
},
error: function (xhr, status, error) {
console.log("An error occured while retrieving cart items");
console.log(xhr.responseText);
},
});
}
updateCartItems();
$("#cart-badge-btn").click(function () {
getCartItemsForModal();
});
function getCartItem() {
$.ajax({
type: "GET",
url: "getCartItem.php",
success: function(response) {
$("#cart-items").html(response);
},
error: function(xhr, status, error) {
alert("An error occurred while retrieving the cart items.");
console.log(xhr.responseText);
}
});
}
updateCartBadge();
getCartItem();
});