In my product detail page I have this code for the user to adjust the quantity they want:
<div class="btn-num-product-down cl8 hov-btn3 trans-04 flex-c-m">
<i class="fs-16 zmdi zmdi-minus"></i>
</div>
<input class="mtext-104 cl3 txt-center num-product" type="number" name="numProduct" value="1">
<div class="btn-num-product-up cl8 hov-btn3 trans-04 flex-c-m">
<i class="fs-16 zmdi zmdi-plus"></i>
</div>
This is the javascript to increase or decrease the value for the button:
$('.btn-num-product-down').on('click', function(){
var numProduct = Number($(this).next().val());
if(numProduct > 0) $(this).next().val(numProduct - 1);
});
$('.btn-num-product-up').on('click', function(){
var numProduct = Number($(this).prev().val());
$(this).prev().val(numProduct + 1);
});
The id, name etc of the product is from the sql database but I do not know how to add this button's value to the PHP session. I made a default quantity of 1 because I do not know how to get the button's value here:
<?php
session_start();
if (isset($_SESSION['cart'])) {
$checker=array_column($_SESSION['cart'], 'item_name');
if (in_array($_GET['cart_name'], $checker)){
echo "<script>alert('Product Is Already In The Cart');
window.location.href='product.php';
</script>";
}else {
$count=count($_SESSION['cart']);
$_SESSION['cart'][$count]=array('item_id' => $_GET['cart_id'], 'item_name'=>$_GET['cart_name'], 'item_price'=>$_GET['cart_price'], 'quantity'=>1 );
echo "<script>alert('Product Added');
window.location.href='product.php';
</script>";
}
} else {
$_SESSION['cart'][0]=array('item_id'=>$_GET['cart_id'], 'item_name'=>$_GET['cart_name'], 'item_price'=>$_GET['cart_price'],'quantity'=>1 );
echo "<script>alert('Product Added');
window.location.href='product.php';
</script>";
}
?>
After the user adjusts the quantity there is a button to carry the values to the final cart page:
<button onclick="location.href='carthandler.php?cart_id=<?php echo $final['id'] ?>&cart_name=<?php echo $final['name'] ?>&cart_price=<?php echo $final['price'] ?>'" class="flex-c-m stext-101 cl0 size-101 bg1 bor1 hov-btn1 p-lr-15 trans-04 js-addcart-detail">
Add to cart
</button>
Can you kindly assist me?