Hi Team
I am having an issue on my cart its duplicating values on the cart. Meaning if i add 1 item at a time it doubles that value to the cart. How do i fix this issue?
// jquery code
$(document).ready(function() {
// Send product details in the server
$(".addItemBtn").click(function(e) {
e.preventDefault();
var $form = $(this).closest(".form-submit");
var pid = $form.find(".pid").val();
var pname = $form.find(".pname").val();
var pprice = $form.find(".pprice").val();
var pimage = $form.find(".pimage").val();
var pcode = $form.find(".pcode").val();
var pqty = $form.find(".pqty").val();
var pqty = 1;
$.ajax({
url: 'action.php',
method: 'post',
data: {
pid: pid,
pname: pname,
pprice: pprice,
pqty: pqty,
pimage: pimage,
pcode: pcode
},
success: function(response) {
$("#message").html(response);
window.scrollTo(0, 0);
load_cart_item_number();
}
});
});
// Load total no.of items added in the cart and display in the navbar
load_cart_item_number();
function load_cart_item_number() {
$.ajax({
url: 'action.php',
method: 'get',
data: {
cartItem: "cart_item"
},
success: function(response) {
$("#cart-item").html(response);
}
});
}
});
// html code
prepare('SELECT * FROM products');
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row):
// Access the data using $row['column_name']
$productID = $row['id'];
$productName = $row['product_name'];
// ...
?>
= $row['product_name'] ?>
= number_format($row['product_price'],2) ?>/-
RonaldPosted May 25, 2023, 2:00 PM
The issue with duplicating values in the cart is caused by adding a fixed quantity of 1 every time the "Add to Cart" button is clicked, regardless of the actual quantity entered by the user. To fix this issue, you should modify your code to retrieve the quantity entered by the user and use that value instead of a fixed quantity of 1. Here's an updated version of your code with the necessary changes:
javascript
Copy code
$(document).ready(function() {
// Send product details to the server
$(".addItemBtn").click(function(e) {
e.preventDefault();
var $form = $(this).closest(".form-submit");
var pid = $form.find(".pid").val();
var pname = $form.find(".pname").val();
var pprice = $form.find(".pprice").val();
var pimage = $form.find(".pimage").val();
var pcode = $form.find(".pcode").val();
var pqty = $form.find(".pqty").val(); // Get the quantity entered by the user
$.ajax({
url: 'action.php',
method: 'post',
data: {
pid: pid,
pname: pname,
pprice: pprice,
pqty: pqty, // Use the entered quantity instead of a fixed value of 1
pimage: pimage,
pcode: pcode
},
success: function(response) {
$("#message").html(response);
window.scrollTo(0, 0);
load_cart_item_number();
}
});
});
// Rest of your code...
});
With this change, the actual quantity entered by the user will be sent to the server and added to the cart, ensuring that duplicate values are not generated. If you can't implement this code, it's worth contacting a specialist in custom software development
RonaldPosted May 25, 2023, 1:59 PM
a
Mudzakkir TohaPosted May 20, 2023, 2:04 AM
Where is this file? Please attach post handler and get handler..
Aravind GovindarajPosted May 19, 2023, 10:56 AM
Hi, Could you please validate what is the response value you are getting while loading into the cart from the below code
$("#cart-item").html(response);
Also please validate how many times the for loop is iterating
foreach ($rows as $row):
This may help you
Mohamed Azarudeen ZPosted May 19, 2023, 9:35 AM
Hi can you check in the questions i guess someone had the same issue.