title: Dynamic Chained SELECT Boxes using jQuery
body: <p>I am using a jQuery/JS/Ajax script to dynamically fill SELECT boxes from my database, with each subsequent SELECT box being populated based on the the previous SELECT.</p>
  
  <p>Eg. Someone selects 'England' from my first SELECT, this then populates the next SELECT with towns in England etc.</p>
  
  <p>The trouble I'm having is getting the second SELECT function to recognise a variable. It's a lot of code so I don't want to paste it all but... this is the first function which populates the first SELECT box on page load:</p>
  
  <pre><code>function getTierOne()
  {   
   $tblname = $_SESSION['country'];
   $result = mysql_query("SELECT DISTINCT county FROM $tblname ORDER BY county") 
   or die(mysql_error()); 
   while($tier = mysql_fetch_array( $result ))  
   {
    echo '&lt;option value="'.$tier['county'].'"&gt;'.$tier['county'].'&lt;/option&gt;';
   }
  }
  </code></pre>
  
  <p>This is fine, $tblname is a posted $_SESSION variable containing the users country, obvs.</p>
  
  <p>The problem is 'onBlur' of this first SELECT box, a second SELECT box is brought up using this JS function and PHP call: </p>
  
  <pre><code>$(document).ready(function() 
  {
      $('#wait_1').hide();
      $('#drop_1').change(function()
          {
              $('#wait_1').show();
              $('#result_1').hide();
              $.get("func.php", 
                  {
                      func: "drop_1",
                      drop_var: $('#drop_1').val()
                  }, 
              function(response)
                  {
                      $('#result_1').fadeOut();
                      setTimeout("finishAjax('result_1', '"+escape(response)+"')", 400);
                  });
              return false;
          });
  });
  </code></pre>
  
  <p>...</p>
  
  <pre><code>if (isset($_GET['func'])&amp;&amp; $_GET['func'] == "drop_1") {
  drop_1($_GET['drop_var']);
  }
  </code></pre>
  
  <p>Which calls this function:</p>
  
  <pre><code>function drop_1($drop_var)
  {  
    include_once('functions.php');
    $result = mysql_query("SELECT DISTINCT town FROM $tblname WHERE county='$drop_var'") 
    or die(mysql_error());
  
    echo '&lt;select name="drop_2" id="drop_2"&gt;
    &lt;option value=" " disabled="disabled" selected="selected"&gt;Select Your Town/Nearest Town&lt;/option&gt;';
  
    while($drop_2 = mysql_fetch_array( $result )) 
    {
    echo '&lt;option value="'.$drop_2['town'].'"&gt;'.$drop_2['town'].'&lt;/option&gt;';
    }
    echo '&lt;/select&gt;';
    echo '&lt;input type="submit" name="submit" value="Continue" /&gt;';
  }
  </code></pre>
  
  <p>But this function will not recognise my $tblname variable, even if i use Global! The only variable it recognises is $drop_var, which is the result of the first SELECT box.</p>
  
  <p>Does anyone have any idea how i can pass the $tblname variable into</p>
  
  <pre><code>function drop_1($drop_var) { ...
  </code></pre>