title: Dynamically populate up to 3 dropdowns based on previous dropdown selections
body: <p>I know similar questions have been asked but I have not found a solution.</p>
  
  <p>I have a <a href="http://jsfiddle.net/sloga/KdskX/23/" rel="nofollow">sample fiddle</a> using some of my known years, months and days as a sample. Instead on manually adding select option divs for each year, I'd like the div id to dynamically populate based on the value of the year selected and so on.</p>
  
  <p>I have all dropdown option values from my database (php, mysql, cakephp 1.3). The first dropdown contains unique years that reports have been entered. Once selected (onchange) perhaps, the second dropdown will only give unique Months that reports were entered for that selected year and then once the month is selected (onchange) a third dropdown will populate with the dates of each day a report was entered. Reports started in August of 2011 and have not been entered every month nor every day since.</p>
  
  <p><strong>Need help with my ajax</strong> to post selected option from first dropdown list to my controller, then get 2nd dropdown options from new query to db (model) based on the posted value. Let's assume user selects 2013 from the first dropdown, then ajax sends "2013" in a variable to my controller, the controller sends value to my Model and my Model queries the database to get distinct months within 2013 where reports where entered. And since the year is not over, the only months that should appear are January thru July. These populate my months dropdown and the user selects May in example. Ajax sends May or "05" to controller -> Model and the model queries DB to get unique days in May, 2013. This populates my third dropdown. I will have a hidden input field that will be all the selected values from the dropdowns in this format: MM/DD/YYYY. When user submits, this redirect user to the reports page for that date and shows all records entered on that day.</p>
  
  <p>Thanks for helping...</p>
  
  <pre><code>    &lt;select name="drop_1" id="drop_1"&gt;
          &lt;option value="" selected="selected" disabled="disabled"&gt;Choose Year&lt;/option&gt;
          &lt;option value=""&gt;Select Year&lt;/option&gt;
          &lt;option value="2011"&gt;2011&lt;/option&gt;
          &lt;option value="2012"&gt;2012&lt;/option&gt;
          &lt;option value="2013"&gt;2013&lt;/option&gt;
      &lt;/select&gt;
  
      &lt;select name="drop_2" id="drop_2"&gt;
          &lt;option value="" selected="selected" disabled="disabled"&gt;Choose Month&lt;/option&gt;
          &lt;option value=""&gt;&lt;/option&gt;
          &lt;option value="01"&gt;January&lt;/option&gt;
          &lt;option value="02"&gt;February&lt;/option&gt;
          &lt;option value="03"&gt;March&lt;/option&gt;
          &lt;option value="04"&gt;April&lt;/option&gt;
          &lt;option value="05"&gt;May&lt;/option&gt;
          &lt;option value="06"&gt;June&lt;/option&gt;
          &lt;option value="07"&gt;July&lt;/option&gt;
      &lt;/select&gt;
  
       &lt;select name="drop_3" id="drop_3"&gt;
          &lt;option value="" selected="selected" disabled="disabled"&gt;Choose Day&lt;/option&gt;
          &lt;option value=""&gt;&lt;/option&gt;
          &lt;option value="05/11/2013"&gt;05/11/2013&lt;/option&gt;
          &lt;option value="05/12/2013"&gt;05/12/2013&lt;/option&gt;
          &lt;option value="05/13/2013"&gt;05/13/2013&lt;/option&gt;
      &lt;/select&gt;
  </code></pre>
  
  <p>--
  this is my jQuery but it is not working...the $.get needs to get the value from my Model, index just iframes a new iteration of my page.</p>
  
  <pre><code>    $(document).ready(function() {
      $('#wait_1').hide();
      $('#drop_1').change(function(){
        $('#wait_1').show();
        $('#result_1').hide();
        $.get("index", {  
          func: "drop_1",
          drop_var: $('#drop_1').val()
        }, function(response){
          $('#result_1').fadeOut();
          setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
        });
          return false;
      });
  });
  
  function finishAjax(id, response) {
    $('#wait_1').hide();
    $('#'+id).html(unescape(response));
    $('#'+id).fadeIn();
  }
  function finishAjax_tier_three(id, response) {
    $('#wait_2').hide();
    $('#'+id).html(unescape(response));
    $('#'+id).fadeIn();
  }
  </code></pre>
  
  <p>--
  here's my actual form excerpt from the view, "index" in cakephp 1.3</p>
  
  <pre><code>&lt;div&gt;
      &lt;!-- Begin Date Selection --&gt;
      &lt;form name="udate" action="/reports/daily/" method="post"&gt;
      &lt;?php
      echo "&lt;select id='drop_1' name='drop_1' title='Use the drop list'&gt;";
      echo '&lt;option value="" disabled="disabled" selected="selected"&gt;'."Choose Year".'&lt;/option&gt;';
          foreach ($years as $select_year) 
          {
          echo '&lt;option value="', $select_year[0]["Year(dated)"], '"&gt;', $select_year[0]["Year(dated)"], '&lt;/option&gt;';
          }
      echo "&lt;/select&gt;";?&gt;
  
      &lt;?php
      echo "&lt;select name='drop_2' id='drop_2' title='Use the drop list'&gt;";
      echo '&lt;option value="" disabled="disabled" selected="selected"&gt;'."Choose Month".'&lt;/option&gt;';
          foreach ($months as $select_month) 
          {
          echo '&lt;option value="', $select_month[0]["Month(dated)"], '"&gt;', $select_month[0]["Month(dated)"], '&lt;/option&gt;';
          }
      echo "&lt;/select&gt;";
      echo "&lt;script type=\"text/javascript\"&gt;
      $('#wait_2').hide();
          $('#drop_2').change(function(){
            $('#wait_2').show();
            $('#result_2').hide();
            $.get(\"index\", {
              func: \"drop_2\",
              drop_var: $('#drop_2').val()
            }, function(response){
              $('#result_2').fadeOut();
              setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
            });
              return false;
          });
      &lt;/script&gt;";?&gt;
  
      &lt;?php
      echo "&lt;select id='drop_3' name='drop_3' title='Use the drop list'&gt;";
      echo '&lt;option value="" disabled="disabled" selected="selected"&gt;'."Choose Day".'&lt;/option&gt;';
          foreach ($days as $select_item) 
          {
          echo '&lt;option value="', $select_item[0]["Days(dated)], '"&gt;', $select_item[0]["Days(dated)], '&lt;/option&gt;';
          }
      echo "&lt;/select&gt;";?&gt;
    &lt;span id="wait_1" style="display: none;"&gt;
      &lt;img alt="Please Wait" src="http://dev.asi.calpoly.edu/img/ajax-loader.gif"/&gt;
  &lt;/span&gt;
  &lt;span id="result_1" style="display: none;"&gt;&lt;/span&gt;
  &lt;span id="wait_2" style="display: none;"&gt;
  &lt;img alt="Please Wait" src="http://dev.asi.calpoly.edu/img/ajax-loader.gif"/&gt;
  &lt;/span&gt;
  &lt;span id="result_2" style="display: none;"&gt;&lt;/span&gt; 
              &lt;?php echo $form-&gt;submit('Submit') ?&gt;
          &lt;/form&gt;
      &lt;/div&gt;&lt;!-- End Date Selection --&gt;
  </code></pre>