When I click on add to cart, It's not working although the button change from 'Add to cart' to 'Adding' meaning the jquery is working.
I am creating an e-commerce with add to cart functionality but when I click on add to cart the products name is not added to the cart. Although the button changes, meaning the json is working but the selection is not working.
add produce in cart (manage_cart.php)
if(isset($_POST["product_code"])) {
foreach($_POST as $key => $value){
$product[$key] = filter_var($value, FILTER_SANITIZE_STRING);
}
$statement = $this->conn->prepare("SELECT product_name, product_price FROM products WHERE product_code=? LIMIT 1");
$statement->bind_param('s', $product['product_code']);
$statement->execute();
$statement->bind_result($product_name, $product_price);
while($statement->fetchAll()){
$product["product_name"] = $product_name;
$product["product_price"] = $product_price;
if(isset($_SESSION["produce"])){
if(isset($_SESSION["produce"][$product['product_code']])) {
$_SESSION["produce"][$product['product_code']]["product_qty"] = $_SESSION["produce"][$product['product_code']]["product_qty"] + $_POST["product_qty"];
} else {
$_SESSION["produce"][$product['product_code']] = $product;
}
} else {
$_SESSION["produce"][$product['product_code']] = $product;
}
}
$total_product = count($_SESSION["produce"]);
die(json_encode(array('produce'=>$total_product)));
}
index.php
<?php
$newstoreobj = $storeobj->select_product();
if(count($newstoreobj)){
foreach ($newstoreobj as $data) {
?>
<form class="product-form">
<div class="col-md-3">
<a href="view_product/product.php?product_id=<?php echo $data['product_id'] ?>/product<?php echo $data['product_name'].'.html'?>">
<div class="prods">
<div style="height:250px">
<img src="upload/<?php echo $data['image1'];?>" class="img-responsive" style="padding:20px;height:100%;width:100%" />
</div>
<div>
<p class="text-center" style="color:black; font-weight: bolder;"><?php echo $data['product_name'];?></p>
<p class="text-center" style="color:red;font-weight:bold;font-size:24px">$<?php echo $data['product_price'];?>.00 </p>
</div>
</a>
<select name="product_qty" style="display: none;">
<option value="1" selected="">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<div class="buton" style="width:100;">
<input name="product_code" type="hidden" value="<?php echo $data["product_code"];?>">
<button type="submit" class="btn btn-block btn-danger">Add to Cart</button>
</div>
</div>
</div>
</form>
<?php
}
}
?>
script.js
// add item to cart
$(".product-form").submit(function(e){
var form_data = $(this).serialize();
var button_content = $(this).find('button[type=submit]');
button_content.html('Adding...');
$.ajax({
url: "manage_cart.php",
type: "POST",
dataType:"json",
data: form_data
}).done(function(data){
$("#cart-container").html(data.products);
button_content.html('Add to Cart');
})
e.preventDefault();
});
when I click on the add to cart button, I want the product to be added to the cart. Thanks
One of reason behind this is that you define die(json_encode(array('produce'=>$total_product)));
in manage_cart.php
It means result is stored in produce
and when you get result in jquery
using data.products
.
Thats why its not getting result.
So please replace name in one place either in manage_cart.php
or jQuery
code