php - I want to overwrite cookie value according to the id

637

In this code i want to overwrite the cookie values according to the id. Once you add one value into cookie of id=55 and again if i am add new value from same id the cookie value updated with new value of same id not add.
Here is my code

<form method='post'>
<input type="text" hidden="hidden" value="<?php echo $prdctid;?>" name="id">
<input type="number" name="name" value="1" min="1" max="10" />
<input type="submit" id="submit" name="submit" value="Add" />
</form>

if(isset($_POST['submit'])){

    $qtyprid = $_POST['id'];

    $qtyval = $_POST['name'];



    $cart_qty=array();

    $cart_qty[$qtyval]=$qtyprid;

    $cookie = $_COOKIE['qty_items_cookie'];

    $cookie = stripslashes($cookie);

    $saved_cart_qty = json_decode($cookie, true);

    if(!$saved_cart_qty){

        $saved_cart_qty=array(); 

    }

    if(count($saved_cart_qty)>0){

        foreach($saved_cart_qty as $key=>$value){

            $cart_qty[$key]=$value;

        }

    }

    $json = json_encode($cart_qty, true);

    setcookie('qty_items_cookie', $json);
}

People are also looking for solutions to the problem: php - strpos: finding a value in .ini file

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.