Dynamic array in jQuery-Collection of common programming errors

I’ve got a problem with this:

if($.cookie('products') == undefined) {
    $.cookie("products", [$(this).data('name')]);
}
else $.cookie("products", [$.cookie('products')+$(this).data('name')]);

My goal is to add names of products to the cookie, cause I need to use them in PHP after sending them through a form.

I’m using array, because then in code I need to delete last object:

$.cookie("products", $.cookie('products').pop());

Or maybe you have another solution to transport variables from jQuery in one file to PHP’s variables in another?

  1. Okey, I managed with main problem using this:

    var products = [$.cookie('products')];
    products.push($(this).data('name'));
    $.cookie("products", products);
    

    But there’s still a problem with pop() method:

    products.pop();
    $.cookie("products", products);
    

    Instead of deleting only a last element it removes the whole array.

Originally posted 2013-11-09 19:11:55.