The code below is a plus and minus button that is suppose to add 1 or minus 1 from the quantity of my shopping cart.
<form method="post" style="margin: 0; display: inline-block;">
<button method="submit" name="plusBtn" value="add">+</button>
<button method="submit" name="minusBtn" value="minus">-</button>
<input type="hidden" name="productID" value="<?php echo $array['shoeId']; ?>">
</form>
This is the function to add or minus 1
function plusQuantity(){
$productId = $_POST['productID'];
foreach($_SESSION['finalCart'] as $eachItem){
if($eachItem['shoeId'] === $productId){
$eachItem['quantity']+1;
}
}
}
function minusQuantity(){
$productId = $_POST['productID'];
foreach($_SESSION['finalCart'] as $eachItem){
if($eachItem['shoeId'] === $productId){
if($eachItem['quantity'] = 1){
unset($eachItem['shoeId']);
} else {
$eachItem['quantity']-1;
}
}
}
}
This is how I am calling the functions
if(isset($_POST['plusBtn'])){
plusQuantity();
print_r($_SESSION['finalCart']);
}
if(isset($_POST['minusBtn'])){
minusQuantity();
}
However the function sort of just load by itself when the page loads and the function doesn't seem to be working also however I added like the print_r($_SESSION['finalCart'] or even some echo texts to the calling of the function part to see if the function is running and indeed it was running by itself. Would be grateful for any help I can get still new to php and trying to learn it Thanks.