0

I have a task which is compare up to five products from the product list. For that I have followed following steps:

step 1:

set onclick event when we click add to compare button of each product. In this event I have set cookie by javascript using this code.

// cookie is set by array because of we have to store 1 to 5 products
var comparearray  = [productid];
document.cookie = "compareitem" + "=" + comparearray;

It is successfully set the cookie value which holds product id of those are selected to compare.

Step 2: In my PHP file I have tried to retrieve this cookie value.BY,

$cookie_val = $_COOKIE['compareitem '];

But it is not worked. I don't know this kind of concept is worth. If know, give me the instructions how to solve my problem. Thanks in advance.

Ajitha Ms
  • 545
  • 5
  • 18
  • Did you try `var_dump` of just `$_COOKIE`? What does that return? – Basit Aug 13 '14 at 11:35
  • 2
    JS and PHP cookies are separate things. Use Ajax to send data to server and save it to `$_COOKIE` – Justinas Aug 13 '14 at 11:35
  • @Justinas Thank you for the information. Let me try – Ajitha Ms Aug 13 '14 at 11:36
  • Have any other soln?? – Ajitha Ms Aug 13 '14 at 11:37
  • Somethimes I had a problem with cookie when js and php use different path. for example js set path to `/` and php try to read cookie from current path, may be `/products` – folibis Aug 13 '14 at 11:40
  • Hi, can you show us how the cookies are stored in JS? Normaly you would stringify the array to JSON before you write the cookie. Next can you show us the var_dump of your $_COOKIE-Array in PHP? Then maybe we could help. – Agash Thamo. Aug 13 '14 at 11:40
  • most likely you have different path for your js and php cookie, check this out http://stackoverflow.com/questions/5045053/set-cookie-wih-js-read-with-php-problem – Exlord Aug 13 '14 at 11:50

2 Answers2

0

Since some people are writing that you can't use cookies set with JS in PHP, I'm going to answer that question now.

Please try to use a cookieSet and cookieGet function, you can use the one from this answer: How do I create and read a value from cookie?

var createCookie = function(name, value, days) {
    var expires;
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

Note the last parameter which is written into the cookie.. the path-param is set to '/', so the root location! Use a php script in the root location, so that both use the root location and not some other location.

Next please try to stringify the array with JSON.

var cookiedata = JSON.stringify(comparearray);

Then you should be able to get the cookie with PHP and parse the JSON to get the array back.

Community
  • 1
  • 1
Agash Thamo.
  • 748
  • 6
  • 18
0

Since it may be problem with path I suggest to set/get cookie with extended way:

document.cookie="foo=bar; path=/;"

or you can use this function:

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
folibis
  • 12,048
  • 6
  • 54
  • 97