Hi Team
I need some help and been stuck to this issue, the issue when inspecting by debugging on the server side i am getting 200 status. But on the client side i am getting failed to add to item. What exactly am i doing wrong?
// client side code
// jquery code
$(document).ready(function() {
$('.add-to-cart-btn').on('click', function(e) {
e.preventDefault();
var id = $(this).attr("id");
var product_item = $("#product-item").val();
var price = $("#price").val();
var product_img = $("#product_img").val();
var quantity = $("#quantity").val();
// Send an AJAX request to update the cart in the database
$.ajax({
url: 'update-cart.php',
method: 'POST',
data: { id: id, product_item:product_item, price:price, product_img:product_img, quantity:quantity},
success: function(response) {
// Handle the response from the server
if (response.success) {
// Update the cart badge count
var cartBadge = $('.fa-shopping-cart + .badge');
var cartCount = parseInt(cartBadge.text());
cartBadge.text(cartCount + 1);
} else {
// Handle the error scenario
alert('Failed to add item to the cart. Please try again.');
}
},
error: function() {
alert('An error occurred while processing your request. Please try again later.');
}
});
});
});
// server side
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Update the cart item with the new quantity or perform other operations
// Example: Update the quantity in the cart table
$stmt = $conn->prepare("UPDATE cart SET quantity = :quantity WHERE id = :itemId");
$stmt->bindParam(':quantity', $quantity);
$stmt->bindParam(':itemId', $id);
$stmt->execute();
// Return a success response
$response = ['success' => true];
echo json_encode($response);
exit; // Stop executing further code
} catch (PDOException $e) {
// Return an error response
$response = ['success' => false, 'error' => $e->getMessage()];
echo json_encode($response);
exit; // Stop executing further code
}
} else {
echo json_encode(['success' => false, 'error' => 'Invalid request']);
}
} else {
echo json_encode(['success' => false, 'error' => 'Invalid request']);
}
?>
Mohamed Azarudeen ZPosted May 16, 2023, 3:37 PM
Hi i think this question is been posted rececntly i answred it kindly check and if you have difficulty finding kindly let me know ill answer again.