1

I want to add values to the existing array variable inside my cookie.

Currently I set my cookie using Ajax:

Ajax:

function setcookie(productid){
     $.ajax({
            type: 'POST',
            url: "setcookies.php",
            data: {
                id: "productid"
            },
            success: function (e) {
                alert(e);
            }
        });
}

PHP setcookies.php

<?php
$cookiename = "products";
$cart = array();

$pid = $_POST['id'];
array_push($cart, $pid);

setcookie($cookiename, serialize($cart), time() + 3600, "/");
$_COOKIE[$cookiename] = serialize($cart);

When I click addproduct button setcookie() function will be called. Clicking addproduct button three times I expected that 3 ids of the product should be added to the cookie array but when I access the page that will show the cookies in my page it will just only show the last productid added.

Thank you in advance guys.

EDIT FOR MY WORKING CODE:

Below code works in my end: Just slightly modified the code answered by Dominique.

$cookiename = "products";
$cart = null;
$pid = $_POST['id'];
if (!empty($_COOKIE[$cookiename])) {
    $cart = unserialize($_COOKIE[$cookiename]);
    array_push($cart, $pid);
} else {
    $cart = array();
    array_push($cart, $pid);
}
setcookie($cookiename, serialize($cart), time() + 3600, "/");
$_COOKIE[$cookiename] = serialize($cart);
3
  • Why don't you set the cookie client side? Commented Jun 6, 2017 at 14:08
  • @ka_lin How will I do that? I'm sorry I'm new to cookies Commented Jun 6, 2017 at 14:10
  • @iamj: see stackoverflow.com/questions/14573223/… on an explanation for seeting cookies client-sided. Commented Jun 6, 2017 at 14:58

2 Answers 2

7

In your code, you instantiate $cart as an array containing 0 element and put it in $_COOKIE['products'] so it'll delete existing content.

It would explain why you could only have an array with a simple element.

This code should work

<?php
$cookiename = "products";

$cart = array();

if(!empty($_COOKIE[$cookiename])) {
    $cart = json_decode($_COOKIE[$cookiename], true);
}

$pid = $_POST['id'];
array_push($cart, $pid);

setcookie($cookiename, json_encode($cart), time() + 3600, "/");
$_COOKIE[$cookiename] = json_encode($cart);
Sign up to request clarification or add additional context in comments.

2 Comments

lemme try that ;)
Thank you for your response. I already fix the problem in my code. I just modified your code a little :)
1
<?php

$pid = $_POST['id'];
$cookiename = "products";

$cart = array();

if(isset($_COOKIE[$cookiename]) && !empty($_COOKIE[$cookiename])) {
    $cart = unserialize(base64_decode($_COOKIE[$cookiename]));
}

array_push($cart, $pid);

setcookie($cookiename, base64_encode(serialize($cart)), time() + 3600, "/");

//Your Cookie data output
$_COOKIE[$cookiename] = serialize($cart);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.