{"id":3016,"date":"2012-05-04T14:07:32","date_gmt":"2012-05-04T12:07:32","guid":{"rendered":"http:\/\/www.beseit.net\/?p=3016"},"modified":"2015-12-18T00:39:16","modified_gmt":"2015-12-17T22:39:16","slug":"passar-un-array-de-javascript-a-php","status":"publish","type":"post","link":"https:\/\/www.beseit.net\/?p=3016","title":{"rendered":"Passar  un array de JavaScript a php"},"content":{"rendered":"<p><iframe loading=\"lazy\" src=\"\/finestres_prog\/PHP\/form_php\/array_JS_php.htm\" width=\"600\" height=\"200\" frameborder=\"0\" scrolling=\"yes\"><\/iframe><\/p>\n<p>array_JS_php.htm<\/p>\n<p>[codesyntax lang=&#8221;html4strict&#8221;]<\/p>\n<pre>&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\r\n\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Language\" content=\"ca\" \/&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;\r\n&lt;title&gt;passsar array JS a php&lt;\/title&gt;\r\n\r\n&lt;script type=\"text\/javascript\"&gt;\r\n\r\nscriptAr = new Array();\r\nscriptAr[0] = \"one\";\r\nscriptAr[1] = \"two\";\r\nscriptAr[2] = \"three\";\r\n\r\n function setValue()\r\n{\r\n\/\/ ho passa a cadena\r\nvar arv = scriptAr.toString();\r\ntest.arv.value = arv ;\r\n\r\n\/\/ ho escriu al formulari de forma oculta per al scrip php\r\n}\r\n&lt;\/script&gt;\r\n \r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;form action=\"phpArrayTest.php\" method=\"post\" name=\"test\" onsubmit=\"setValue()\"&gt;\r\n\r\n&lt;p&gt;\r\nAquest exemple crea&amp;nbsp; l&amp;#39;array scriptAr[0] = &amp;quot;one&amp;quot;; scriptAr[1] = &amp;quot;two&amp;quot;; scriptAr[2] = &amp;quot;three&amp;quot;. &lt;br \/&gt;\r\nQuan  \r\n\r\ns&amp;#39;envia el formulari demes de l&amp;#39;acci\u00f3 &amp;quot;phpArrayTest.php&amp;quot; tamb\u00e9 s&amp;#39;executa la \r\nfunci\u00f3 JS &amp;quot;setValue()&amp;quot;. Mirar codi posant el ratol\u00ed \r\n&lt;span style=\"color: #FF0000\"&gt;aqu\u00ed&lt;\/span&gt; i premen lo \r\nboto dret  \r\n\r\n.&lt;br \/&gt;\r\nLa funci\u00f3 setValue() passa l&amp;#39;array a un camp ocult del formulari de nom \r\n&lt;span style=\"font-weight: 700\"&gt;arv&lt;\/span&gt; . Es a dir el prepara per a que \r\nl&amp;#39;acci\u00f3 php ho pugui llegir. &lt;\/p&gt;\r\n\r\n\r\n\r\n\r\n&lt;input name=\"arv\" type=\"hidden\" \/&gt;\r\n&lt;input type=\"submit\" value=\"enviar formulari\"\/&gt;\r\n\r\n&lt;\/form&gt; \r\n\r\n&lt;p&gt;\r\nCodi de l'acci\u00f3 phpArrayTest.php:&lt;\/p&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>[\/codesyntax]<\/p>\n<p>phpArrayTest.php<\/p>\n<p>[codesyntax lang=&#8221;php&#8221;]<\/p>\n<pre>&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\r\n\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/&gt;\r\n&lt;title&gt;Sin t\u00edtulo 1&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;?php\r\n\r\n$ss = $_POST['arv']; \r\n\/\/echo \" valor de ss= \". $ss;\r\n\/\/ La funcion explode convertira la cadena a arreglo \r\n\/\/******************************\r\n$tok = explode(',',$ss); \r\n\/\/******************************\r\n\/\/ per a visualitzar-la la tornem a possar en una l\u00ecnia\r\necho  implode(',',$tok);\r\n\r\n?&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>[\/codesyntax]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>array_JS_php.htm [codesyntax lang=&#8221;html4strict&#8221;] &lt;!DOCTYPE html PUBLIC &#8220;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&#8221; &#8220;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&#8221;&gt; &lt;html xmlns=&#8221;http:\/\/www.w3.org\/1999\/xhtml&#8221;&gt; &lt;head&gt; &lt;meta http-equiv=&#8221;Content-Language&#8221; content=&#8221;ca&#8221; \/&gt; &lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=utf-8&#8243; \/&gt; &lt;title&gt;passsar array JS a php&lt;\/title&gt; &lt;script type=&#8221;text\/javascript&#8221;&gt; scriptAr = new Array(); scriptAr[0] = &#8220;one&#8221;; scriptAr[1] = &#8220;two&#8221;; &hellip; <a href=\"https:\/\/www.beseit.net\/?p=3016\">Continua llegint <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71],"tags":[],"class_list":["post-3016","post","type-post","status-publish","format-standard","hentry","category-javascript-sql"],"_links":{"self":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/3016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3016"}],"version-history":[{"count":20,"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/3016\/revisions"}],"predecessor-version":[{"id":6422,"href":"https:\/\/www.beseit.net\/index.php?rest_route=\/wp\/v2\/posts\/3016\/revisions\/6422"}],"wp:attachment":[{"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beseit.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}