title: how to handle checkboxes with statements and echo them
body: <p>hello i have the following problem:</p>
  
  <p>i would like to create a filter for a search engine. i have some inputfields for specific search terms and beside a respective checkbox.  so that looks like:</p>
  
  <p>inputfield a:   [<strong><em>_</em>____</strong>]    filter for a: on/off: [ ]</p>
  
  <p>inputfield b:   [<strong><em>_</em>____</strong>]    filter for b: on/off: [ ]</p>
  
  <p>inputfield c:   [<strong><em>_</em>____</strong>]    filter for c: on/off: [ ]</p>
  
  <p>the code structure of that is:</p>
  
  <p>first of all check if the inputfield is empty and the checkbox is set checked. in case of checked it will be unchecked after submit. the other way around, if just the inputfield is filled and the checkbox unchecked, it also will error out, that there is a mistake and the filter can't work. so i'm using for each filter an own error array like <code>(the name and value of the first checkbox is name="filter_a" value="1")</code> :</p>
  
  <pre><code>...
  $checkbox_filter_a = $db-&gt;real_escape_string(htmlentities(trim($_POST['filter_a'])));
  ...
  $filter_a = (!empty($checkbox_filter_a)) ? 1 : 0;
  ...
  $errors_a = array();
  if ($filter_a == 1){
      if (empty($input_a)){
          $errors_a[] = 'the filter needs some input';
      }
  }
  
  if (!empty($input_a)){
      if ($filter_a == 0){
          $errors_a[] = 'filter is not activated';
      }
  }
  </code></pre>
  
  <p>if there is no error and both criteria are fulfilled the filter either is on or not. 
  so the logical behind that is, when loading the page the checkbox have to be unchecked. after regarding the criteria to filter it is on.</p>
  
  <p>to display the status checked or unchecked under the conditions below it should be checked after submit or either not. therefor i have this code for each filter (respective checkbox_filter_b, ...) the part of the checkbox after the inputfield:</p>
  
  <pre><code>&lt;?php
  if (checkbox_filter_a == 0 ) {
       echo '&lt;input type="checkbox" name="checkbox_filter_a" value="1" checked/&gt;';
  }else {
       echo '&lt;input type="checkbox" name="checkbox_filter_a" value="1"/&gt;';
  }
  ?&gt;
  </code></pre>
  
  <p>what does not satisfy at all. this is because of the following problem:</p>
  
  <p>when loading the page it will show all checkboxes are unchecked. if i try to cause an error because of no filled input or just checked checkbox for one of these filters, after submit all other checkboxes are automatically checked. </p>
  
  <p>so if there is someone who could help me out i really would appreciate. thanks a lot.</p>