Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
Updating cookie on change from number input

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/

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda