Muhammad Hassan
Muhammad Hassan

Reputation: 1

$_SESSION variable in php is not incrementing according to the condition

This a code snippet of my project, an ecommerce store. I have created a javascript event to increment the quantity of product. The quantity of product is increasing accordingly but the value of my $_SESSION variable is not increasing, this variable will be used to insert the quantity of product in the cart and deduct desired quantity form the database.

Following is php code:

$qty = 1;
$_SESSION['qty'] = $qty;

Following is javascript code:

let stock = document.querySelector(".stockNumber");
let plus = document.querySelector(".plus");
let minus = document.querySelector(".minus");
var number = 1;

if (<?php echo $row['quantity']; ?> <= 0) {
    $(".warn3").show();
}
else {
    plus.addEventListener('click', function () {
        if (number < <?php echo $row['quantity']; ?>) {
            $(".warn2").hide();
            number++;
            stock.textContent = number;
            <?php $qty = $qty+1; ?>
        }
        else {
            $(".warn1").show();
            setTimeout(function () {
                $(".warn1").hide();
            }, 3000)
        }
    })

    minus.addEventListener('click', function () {
        if (number > 1) {
            number--;
            stock.textContent = number;
            <?php $qty = $qty-1; ?>
            $(document).ready(function () {
                $(".warn1").hide();
            })
        }
        else {
            $(".warn2").show();
            setTimeout(function () {
                $(".warn2").hide();
            }, 3000)
        }
    })
}

I want the $_SESSION variable to be incremented according to increase in the quantity of product.

Upvotes: 0

Views: 18

Answers (0)

Related Questions