{"id":4253,"date":"2022-10-31T14:32:32","date_gmt":"2022-10-31T14:32:32","guid":{"rendered":"https:\/\/ibex.tech\/javascript\/?p=4253"},"modified":"2022-10-31T14:34:00","modified_gmt":"2022-10-31T14:34:00","slug":"form-state-save-and-reload-from-javascript","status":"publish","type":"post","link":"https:\/\/ibex.tech\/javascript\/forms-javascript\/form-state-save-and-reload-from-javascript","title":{"rendered":"Form state save and reload from javascript"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Save the form in PHP<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/----- GET THE SUBMITTED FORM SETUP -----\n    $FormSetup = $_POST;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Restore the form<\/h4>\n\n\n\n<p>We use PHP variables to do it optionally (they are only setup if the form setup is to be restored)<\/p>\n\n\n\n<p>N.B. This code is only handling checkboxes and select boxes currently<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">PHP<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $FormSetup = Null;\n  $LoadSavedSetupScriptHtml = \"\";\n  $LoadSavedSetupScriptCallOnLoadHtml = \"\";\n\n  $FormSetup = \/\/&lt;&lt;Optionally load the array back here\n\n  if (is_array($FormSetup))\n  {    \n    \/\/----------------------------------------------------------\n    \/\/----- CREATE JAVASCRIPT TO SETUP THE FORM CHECKBOXES -----\n    \/\/----------------------------------------------------------\n    $LoadSavedSetupScriptCallOnLoadHtml = \"LoadSavedFormSetup();\";\n\n    $LoadSavedSetupScriptHtml = \"function LoadSavedFormSetup() {\\n\";\n    foreach ($FormSetup as $FormSetupItem => $NextElement)\n    {\n        $LoadSavedSetupScriptHtml .= &lt;&lt;&lt;_END\n\n          var NextElement = document.getElementById(\"$FormSetupItem\");\n\n          if (NextElement)\n          {            \n            if (NextElement.tagName.toLowerCase() === 'input' &amp;&amp; NextElement.getAttribute('type') === 'checkbox') \n              NextElement.checked = true;\\n\n            else if (NextElement.tagName.toLowerCase() === 'select')\n              NextElement.value = \"$NextElement\"\n          }\n_END;\n    }\n    $LoadSavedSetupScriptHtml .= \"  }\";\n  }<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">HTML<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script&gt;\n \n  $LoadSavedSetupScriptHtml\n\n  \/\/Call it also when page loads\n  window.onload = function() {\n    \n    $LoadSavedSetupScriptCallOnLoadHtml\n\n  };\n&lt;\/script&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Save the form in PHP Restore the form We use PHP variables to do it optionally (they are only setup if the form setup is to be restored) N.B. This code is only handling checkboxes and select boxes currently PHP HTML<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-4253","post","type-post","status-publish","format-standard","hentry","category-forms-javascript"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4253","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/comments?post=4253"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4253\/revisions"}],"predecessor-version":[{"id":4256,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/4253\/revisions\/4256"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/media?parent=4253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/categories?post=4253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/tags?post=4253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}