{"id":417,"date":"2022-08-30T15:01:00","date_gmt":"2022-08-30T15:01:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/dynamic-array-in-jquery-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:00","modified_gmt":"2022-08-30T15:01:00","slug":"dynamic-array-in-jquery-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/dynamic-array-in-jquery-collection-of-common-programming-errors\/","title":{"rendered":"Dynamic array in jQuery-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve got a problem with this:<\/p>\n<pre><code>if($.cookie('products') == undefined) {\n    $.cookie(\"products\", [$(this).data('name')]);\n}\nelse $.cookie(\"products\", [$.cookie('products')+$(this).data('name')]);\n<\/code><\/pre>\n<p>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.<\/p>\n<p>I&#8217;m using array, because then in code I need to delete last object:<\/p>\n<pre><code>$.cookie(\"products\", $.cookie('products').pop());\n<\/code><\/pre>\n<p>Or maybe you have another solution to transport variables from jQuery in one file to PHP&#8217;s variables in another?<\/p>\n<ol>\n<li>\n<p>Okey, I managed with main problem using this:<\/p>\n<pre><code>var products = [$.cookie('products')];\nproducts.push($(this).data('name'));\n$.cookie(\"products\", products);\n<\/code><\/pre>\n<p>But there&#8217;s still a problem with pop() method:<\/p>\n<pre><code>products.pop();\n$.cookie(\"products\", products);\n<\/code><\/pre>\n<p>Instead of deleting only a last element it removes the whole array.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:11:55. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve got a problem with this: if($.cookie(&#8216;products&#8217;) == undefined) { $.cookie(&#8220;products&#8221;, [$(this).data(&#8216;name&#8217;)]); } else $.cookie(&#8220;products&#8221;, [$.cookie(&#8216;products&#8217;)+$(this).data(&#8216;name&#8217;)]); 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&#8217;m using array, because then in code I need to delete last object: $.cookie(&#8220;products&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-417","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=417"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/417\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=417"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}