title: PHP & Ajax - can i use POST to retrieve form value without reload
body: <p>I have a php file "Criteria" to display a bunch of html multi select dropdowns that can be used to create criteria for a query.</p>
  
  <p>I have a php file "Results" which builds an SQL statement according to the selected options from various dropdowns, executes the query, and then displays the results of that query.</p>
  
  <p>I would like to skip having to reload &amp; echo all the html for each select dropdown, as they each contain hundreds of options.  Ajax was my solution for this.</p>
  
  <p>For each dropdown, an onchange event calls my javascript, which executes an Ajax call to my "Results" page.  This "Results" page would then get the value of each select dropdown using $_POST['htmlSelectDropdownID'].</p>
  
  <p>The problem, now that I have implemented Ajax, is that the selected value from the select dropdowns is not coming across to my Results file... and I can only assume this is because the HTML never reloaded...</p>
  
  <p>Does anybody have a suggestion of how to resolve this issue?</p>
  
  <p>Thanks,</p>
  
  <p>------------------------ code --------------------------</p>
  
  <p>My Javascript / Ajax</p>
  
  <pre><code> function AjaxPostback(){
  /*if (str=="")
  {
      document.getElementById("divTest").innerHTML="";
      return;
  } */
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
      if (xmlhttp.readyState==4)// &amp;&amp; xmlhttp.status==200)
      {
          document.getElementById("divResults").innerHTML=xmlhttp.responseText;
      }
  }
  xmlhttp.open("GET","Results.php", true); //?q="+str,true);
  xmlhttp.send();}
  </code></pre>
  
  <p>Just 1 of the dropdown on my page</p>
  
  <pre><code>Group By &lt;select id="selGroupBy" name="selGroupBy" onchange="AjaxPostback()"&gt;
              &lt;option value='Department' &lt;?php if ($_POST['selGroupBy'] == 'Department') echo 'selected="selected"'; ?&gt;&gt;Department&lt;/option&gt;
              &lt;option value='Store' &lt;?php if ($_POST['selGroupBy'] == 'Store') echo 'selected="selected"'; ?&gt;&gt;Store&lt;/option&gt;
              &lt;option value='Date' &lt;?php if ($_POST['selGroupBy'] == 'Date') echo 'selected="selected"'; ?&gt;&gt;Date&lt;/option&gt;
              &lt;option value='Size' &lt;?php if ($_POST['selGroupBy'] == 'Size') echo 'selected="selected"'; ?&gt;&gt;Size&lt;/option&gt;
          &lt;/select&gt;
  </code></pre>
  
  <p>-- 
  The html that builds the Criteria (dropdown html) and the div (divResults) who's innerHTML is filled with the javascript</p>
  
  <pre><code>&lt;div&gt;
              &lt;?php include ("Criteria.php"); ?&gt;
  
              &lt;div class="divResults" id="divResults"&gt;
                  &amp;nbsp;
              &lt;/div&gt;
          &lt;/div&gt;
  </code></pre>
  
  <p>When called, my Results.php file simply checks to document for things like..
  if (isset($_POST['selDepartment']) &amp;&amp; ($_POST['selDepartment'] == "320 Socks") {
    $sqlSelect .= $_POST['selDepartment'];
  }</p>
  
  <p>I have also tried using $_GET instead of $_POST to retrieve the value, same result.. =\</p>