i have a shopping cart script that sends it's value across site through cookies. So for example if i click on product page on add to cart, it will put 1 item in cart, but if i go to cart page, and update field with input type number, i dont know how to pass it to the cookie. this is my input field:
<div class="product-quantity">
<input type="number" name="problematican" id="problematican" value="<?php echo number_format($values["stanje1"]); ?>" min="1" >
</div>
And i can read cookie but it's value is constantly 1, only if i do 1 more time add to cart i can get another one. I need heads up on how it can be solved, so that even when i click on arrow up or down or even put number in field, it updates cookie automatically?
Cookies are set through php, and the one in charge of value is shopping_cart['stanje1']
setcookie data:
if(isset($_POST["add_to_cart"]))
{
if(isset($_COOKIE["shopping_cart"]))
{
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
}
else
{
$cart_data = array();
}
$item_id_list = array_column($cart_data, 'item_id');
if(in_array($_POST["hidden_fid"], $item_id_list))
{
foreach($cart_data as $keys => $values)
{
if($cart_data[$keys]["item_id"] == $_POST["hidden_fid"])
{
$cart_data[$keys]["stanje1"] = $cart_data[$keys]["stanje1"] + $_POST["quantity"];
}
}
}
else
{
$item_array = array(
'tip_artikla' => $_POST["tip_artikla"],
'item_id' => $_POST["hidden_id"],
'item_fid' => $_POST["hidden_fid"],
'naziv_proizvoda' => $_POST["hidden_name"],
'cena' => $_POST["hidden_price"],
'slikas' => $_POST["slikas"],
'stanje1' => $_POST["quantity"]
);
$cart_data[] = $item_array;
}
$item_data = json_encode($cart_data, JSON_UNESCAPED_UNICODE);
setcookie('shopping_cart', $item_data, time() + (86400 * 30), '/' );
UPDATE: here is a jsfiddle of what i came up with but for some reason i cant work it out: https://jsfiddle.net/qju7c6dn/2/