Hi Team
I have a button that adds an item but its not increment nor decrementing, can anyone can help with this? I am using jquery and php pdo to do. Below is my logic both front end and back end. How do i also show quantity to the table and total, when this is to be send to checkout page?
// html code
Code
Toggle search
Home
Shopping cart
Shopping cart
You currently have
Product
Quantity
Unit price
Discount
Total
White Blouse Armani
+
-
Black Blouse Armani
Add to Cart
Total// jquery code
JavaScript
$(document).on('click', '.update-cart', function() {
var product_id = $(this).data('product-id');
var type = $(this).data('type');
var quantity = parseInt($('#cart-count-item'+product_id).text());
if (type == 'add') {
quantity += 1;
} else if (type == 'subtract') {
quantity -= 1;
if (quantity < 0) {
quantity = 0;
}
}
// Send AJAX request to update cart quantity
$.ajax({
url: 'update_cart.php',
type: 'POST',
data: {product_id: product_id, quantity: quantity},
success: function(response) {
// Update cart count
$('#cart-count').text(response);
$('#cart-count-item'+product_id).text(quantity);
}
});
});// php code
PHP

Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 19, 2023, 12:37 PM EDT
$(document).on('click', '.update-cart', function() { var product_id = $(this).data('product-id'); var type = $(this).data('type'); var quantity = parseInt($('#cart-count-item'+product_id).text()); if (type == 'add') { quantity += 1; } else if (type == 'subtract') { quantity -= 1; if (quantity < 0) { quantity = 0; } } // Send AJAX request to update cart quantity $.ajax({ url: 'update_cart.php', type: 'POST', data: {product_id: product_id, quantity: quantity}, success: function(response) { // Update cart count $('#cart-count').text(response); $('#cart-count-item'+product_id).text(quantity); } }); });Guest UserRank #877 · 2.5K reputationPosted Apr 19, 2023, 6:51 PM EDT
Hi Swain
The code is not updating, let me show you the full code of html, the total is giving an error because the example is manual values must fetch from the table 'product' colum total to give total amount calculate.
// hml code
// jquery code
$(document).on('click', '.update-cart', function() { var product_id = $(this).data('product-id'); var type = $(this).data('type'); var quantity = parseInt($('#cart-count-item'+product_id).text()); if (type == 'add') { quantity += 1; } else if (type == 'subtract') { quantity -= 1; if (quantity < 0) { quantity = 0; } } // Send AJAX request to update cart quantity $.ajax({ url: 'add_to_cart.php', type: 'POST', data: {product_id: product_id, quantity: quantity}, success: function(response) { // Update cart count $('#cart-count').text(response); $('#cart-count-item'+product_id).text(quantity); } }); });// php code