title: How do I keep value in select box after form submit using a session variable in PHP
body: <p>So I am building an example search page with PHP. It is a job search page with several web forms. When one of these is submitted I want to be able to save the values. I am trying to save them in a session variable and reload the select list, however I can't get it to work. Anyone know what I am doing wrong? Here is my code:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;?php
      session_start();
  
      if(isset($_POST['occupation']))
          $_SESSION['occupation'] = $_POST['occupation'];
  
      if(isset($_POST['work_type']))
          $_SESSION['work_type'] = $_POST['work_type'];
  
      if(isset($_POST['sort_by']))
          $_SESSION['sort_by'] = $_POST['sort_by'];
  
  ?&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;style&gt;
          table
          {
              width: 50%; margin: 0 auto;
          }
          .td-left
          {
              width: 20%;
          }
      &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;table&gt;
      &lt;tr&gt;
          &lt;td class="td-left" valign="top"&gt;
              &lt;form method="post" action=""&gt;
                  &lt;h2&gt;Keyword&lt;/h2&gt;
                  &lt;input type="text" name="keyword"/&gt;
                  &lt;h2&gt;Industy&lt;/h2&gt;
                  &lt;select name="occupation[]" multiple &gt;
                      &lt;?php 
                          $occ = array("Accounting", "Education", "Healthcare", "Information Technology", "Retail", "Sales");
  
                          $len = count($occ);
  
                          for($i = 0; $i&lt;$len; $i++ )
                          {
                              if(in_array($occ, $_SESSION['occupation']))
                                  echo '&lt;option value="' . $i . '" selected&gt;' . $occ[$i] .'&lt;/option&gt;';
                              else
                                  echo '&lt;option value="' . $i . '" &gt;' . $occ[$i] .'&lt;/option&gt;';
                          } 
                      ?&gt;
                  &lt;/select&gt;
                  &lt;br/&gt;&lt;br/&gt;
                  &lt;button type="submit" value="Search" class="my-button" name="search_button" &gt;Search&lt;/button&gt;
              &lt;/form&gt;
          &lt;/td&gt;
          &lt;td style="float: right; " valign="top"&gt;
              &lt;form method="post" action=""&gt;
                  &lt;h2&gt;Sort By&lt;/h2&gt;
                  &lt;select name="sort_by" onchange="this.form.submit()" &gt;
  
                      &lt;?php 
                          $sort_types = array("Date posted descending", "Date posted ascending", "Job title descending", "Job title ascending", "Location descending", "Location ascending", "Salary descending", "Salary ascending" );
  
                          $len = count($sort_types);
  
                          for($i = 0; $i&lt;$len; $i++ )
                          {
                              if(in_array($sort_types, $_SESSION['sort_by'])) 
                                  echo '&lt;option value="' . $i . '" selected&gt;' . $sort_types[$i] .'&lt;/option&gt;';
                              else
                                  echo '&lt;option value="' . $i . '" &gt;' . $sort_types[$i] .'&lt;/option&gt;';
                          } 
                      ?&gt;
                  &lt;/select&gt;
              &lt;/form&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td class="td-left" valign="top"&gt;
              &lt;form method="post" action="" name="refine_search"&gt;
                  &lt;h2&gt;Refine Search&lt;/h2&gt;
                  &lt;select name="work_type[]" multiple &gt;
                      &lt;?php 
                          $wt = array("Full Time", "Part Time", "Casual");
  
                          $len = count($wt);
  
                          for($i = 0; $i&lt;$len; $i++ )
                          {
                              if(in_array($wt, $_SESSION['work_type']))
                                  echo '&lt;option value="' . $i . '" selected&gt;' . $wt[$i] .'&lt;/option&gt;';
                              else
                                  echo '&lt;option value="' . $i . '" &gt;' . $wt[$i] .'&lt;/option&gt;';
                          } 
                      ?&gt;
                  &lt;/select&gt;
                  &lt;br/&gt;&lt;br/&gt;
                  &lt;button type="submit" value="Go" class="my-button" name="refine_search_button"&gt;Go&lt;/button&gt;
              &lt;/form&gt;
          &lt;/td&gt;
          &lt;td valign="top"&gt;
              &lt;h2&gt;Results&lt;/h2&gt;
              Lorem Ipsum Dolor Sit Amet ...
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>