title: Make checkbox array stay checked after submitting the form
body: <p>I create a large form of checkboxes that get their values from the database. The problem is, I need to find a way how to make the browser remember that certain checkboxes were checked. After submitting the form, it posts the values and another function uses the post values for another function.</p>
  
  <p><strong>So this is how I create my checkbox form:</strong></p>
  
  <pre><code>$query .= "SELECT id, ediens FROM menu_edieni_otrie";
      $query1 .= "SELECT id, ediens FROM menu_edieni_zupas";
  
      echo "&lt;body&gt;\n";
      echo "&lt;form name='otrie_edieni' action='' method='post'&gt;\n";
      echo "&lt;table border='1px'&gt;\n";
      echo "&lt;tr&gt;\n";
      echo "&lt;td&gt;\n";
      /* execute multi query */
      if ($mysqli-&gt;multi_query($query)) {
          if ($result = $mysqli-&gt;store_result()) {
              while ($row = $result-&gt;fetch_row()) {
              echo "&lt;input type='checkbox' name='ediens2[]' value='";
              printf ("%s", $row[1]);
              echo "' ";
              echo "/&gt;";
              printf ("%s &lt;/br&gt;\n ", $row[1]);
              }
              $result-&gt;free();
          }
      }
      echo "&lt;/td&gt;\n";
      echo "&lt;td&gt;\n";
      if ($mysqli-&gt;multi_query($query1)) {
          if ($result = $mysqli-&gt;store_result()) {
              while ($row = $result-&gt;fetch_row()) {
              echo "&lt;input type='checkbox' name='zupas2[]' value='";
              printf ("%s", $row[1]);
              echo "'/&gt;";
              printf ("%s &lt;/br&gt;\n ", $row[1]);
              }
              $result-&gt;free();
          }
      }
      echo "&lt;/td&gt;\n";
      echo "&lt;/tr&gt;\n";
      echo "&lt;/table&gt;";
      echo "&lt;input type='submit' name='formSubmit' value='Submit' /&gt;";
      echo "&lt;/form&gt;\n";
      echo "&lt;/body&gt;";
  </code></pre>
  
  <p>How can I check what checkboxes where checked after the submit button is pressed and leave them checked afterwards - after refresh? </p>
  
  <p>The _POST is received by this function:</p>
  
  <pre><code>$ediens2 = $_POST['ediens2'];
  $zupas2 = $_POST['zupas2'];
    if(empty($ediens2) &amp;&amp; empty($zupas2)) 
    {
      echo("Nav ievadīta neviena vērtība");
    } 
    else
    {
      $N = count($ediens2);
      $M = count($zupas2);
      $mysqli = @new mysqli('*', '*', '*', '*');
  
          $mysqli-&gt;query("SET NAMES 'utf8'", $connection);
  
          if ($mysqli-&gt;connect_errno) {
              die('Connect Error: ' . $mysqli-&gt;connect_errno);
          }
      for($i=0; $i &lt; $N; $i++){
          $query3 = "UPDATE tmp_cirks SET name='$ediens2[$i]' WHERE id='$i'";
          $mysqli-&gt;multi_query($query3);
      }
      /* close connection */
      $mysqli-&gt;close();
      $date = date('d/m/Y', time());
      echo $date;
      echo("&lt;br&gt;");
      echo("&lt;ul&gt;");
      for($i=0; $i &lt; $N; $i++)
      {
          echo("&lt;li&gt;");
          echo("&lt;strong&gt;" . $ediens2[$i] . "&lt;/strong&gt;");
          echo("&lt;/li&gt;");
      }
      echo("&lt;/ul&gt;");
      echo("&lt;ul&gt;");
      for($i=0; $i &lt; $M; $i++)
      {
          echo("&lt;li&gt;");
          echo("&lt;strong&gt;" . $zupas2[$i] . "&lt;/strong&gt;");
          echo("&lt;/li&gt;");
      }
      echo("&lt;/ul&gt;");      
    }
  </code></pre>
  
  <p>What I have figured out so far, is that I store the values received from the _POST to a separate database table - with this:</p>
  
  <pre><code>for($i=0; $i &lt; $N; $i++){
          $query3 = "UPDATE tmp_cirks SET name='$ediens2[$i]' WHERE id='$i'";
          $mysqli-&gt;multi_query($query3);
      }
  </code></pre>
  
  <p>I was thinking that then I could compare if the generated checkbox form has any values stored in this separate table. If it has - then it makes the "checked='checked'" function. Am I on the right track?</p>
  
  <p>The database tables look like this:
  id  ediens  edieni_ru   edieni_eng  cena    cena_puse
  otr1    Kotletes    Котлеты Сutlet  2,00    1,30
  otr2    Gulašs  Гуляш   Goulash 2,20    1,40
  otr3    Slinkie tīteņi  Ленивые голубцы Chopped cabbage with fried minced meat  1,70    1,15
  otr4    Plovs   Плов    Pilaff  1,70    1,15</p>
  
  <p>to generate the checkbox values I use only the "<strong>ediens</strong>" column.
  The new table that I have created looks like this (the one that saves the values).</p>
  
  <p>id  name
  1   Kotletes
  2   Gulašs
  3   Karbonāde</p>
  
  <p>so what I was thinking is, something like this:
  if (name==ediens) echo 'checked=checked'; (of course this is just a not working example)</p>
  
  <p>any suggestions? I have no idea if I am even on the right track.</p>
  
  <p>Thank you in advance for the help!</p>