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
JavaScript
$(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
PowerShell
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'];
// ...
?>
" class="card-img-top" height="250">
/-
Quantity :
">
">
">
">
">
">
Add to
cart
Ronald13 reputationPosted May 25, 2023, 10:00 AM EDT
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
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted May 19, 2023, 5:35 AM EDT
Hi can you check in the questions i guess someone had the same issue.
Aravind GovindarajRank #430 · 5.8K reputationPosted May 19, 2023, 6:56 AM EDT
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
Mudzakkir TohaRank #3,303 · 435 reputationPosted May 19, 2023, 10:04 PM EDT
Where is this file? Please attach post handler and get handler..
action.phpRonald13 reputationPosted May 25, 2023, 9:59 AM EDT
a