Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda