-2

I encode array of objects to cookie in JS:

prod.name = name;
prod.id = id;
prod.price = cost;
prod.quantity = 1;
Products = [prod];
Cookies.set('cart', Products);

And name looks like "Some product name", but when i'm doing same thing from PHP:

setcookie('products', json_encode($Products));

I got name like "Some+product+name" - is there any way to avoid it, or the only way is to str_replace() + with " " (what is unicode analog for theme)?

UPD. To be simple:

If I setcookie ('cartNew', json_encode(['name' => "Some product name"])); - in cookie I see "+", and JS decode that as "+". Cookie is like %7B%22name%22%3A%22Some+product+name%22%7D - firefox. Also I use UTF-8, and php 5.4

9
  • 1
    can you put your json encoded data here in your code. thanks. Commented Jun 3, 2015 at 13:31
  • And please put $Products value. thanks Commented Jun 3, 2015 at 13:32
  • 3
    "I got name like "Some+product+name" " "Got" it how? Cookies are URI-encoded. Whatever you're using to "get" it will need to decode it. Commented Jun 3, 2015 at 13:35
  • Please put your $Products value here in your code for solution. thanks. Commented Jun 3, 2015 at 13:36
  • @frenzy It's not really a good idea to store data in a cookie. The best approach is to store a session id in the cookie and then store the data on the server side bound on that ID. When that is said it would properly also make more sense to store the cart's in a database and only store the cart id in the server session storage. Commented Jun 3, 2015 at 13:36

1 Answer 1

-1

Use a RegEx replace to get rid of the the extra '+'s:

prod.name = name.replace(/\+/g, "");

Sign up to request clarification or add additional context in comments.

2 Comments

Don't fix the problem after the fact. Figure out where the +s are coming from in the first place and get rid of them there. Something's being en-/decoded in the wrong order here, that's the real issue.
PHP json_encode replaces " " with "+", not adding new

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.