Save the form in PHP

    //----- GET THE SUBMITTED FORM SETUP -----
    $FormSetup = $_POST;

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
  $FormSetup = Null;
  $LoadSavedSetupScriptHtml = "";
  $LoadSavedSetupScriptCallOnLoadHtml = "";

  $FormSetup = //<<Optionally load the array back here

  if (is_array($FormSetup))
  {    
    //----------------------------------------------------------
    //----- CREATE JAVASCRIPT TO SETUP THE FORM CHECKBOXES -----
    //----------------------------------------------------------
    $LoadSavedSetupScriptCallOnLoadHtml = "LoadSavedFormSetup();";

    $LoadSavedSetupScriptHtml = "function LoadSavedFormSetup() {\n";
    foreach ($FormSetup as $FormSetupItem => $NextElement)
    {
        $LoadSavedSetupScriptHtml .= <<<_END

          var NextElement = document.getElementById("$FormSetupItem");

          if (NextElement)
          {            
            if (NextElement.tagName.toLowerCase() === 'input' && NextElement.getAttribute('type') === 'checkbox') 
              NextElement.checked = true;\n
            else if (NextElement.tagName.toLowerCase() === 'select')
              NextElement.value = "$NextElement"
          }
_END;
    }
    $LoadSavedSetupScriptHtml .= "  }";
  }
HTML
<script>
 
  $LoadSavedSetupScriptHtml

  //Call it also when page loads
  window.onload = function() {
    
    $LoadSavedSetupScriptCallOnLoadHtml

  };
</script>
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *