javascript - In PHP and jQuery, make one drop-down depend on another drop-down list? -


i have 2 drop down lists 1 depended on other if 1 value selected other 1 load same values database. example if select 1 country other load same cities country.

<select name="a" class="input_text" id="a">  <?php include 'config/config.php';  $sql="select * department order dept asc"; $result=mysql_query($sql);$options=""; while ($row=mysql_fetch_array($result)){     $did=$row["deptcode"]; $depts=$row["dept"]; $options.="<option value='$did'>".$depts;}?>  <option value="0">select...</option> <?php echo $options; ?>'  </option> </select>  <select name="b" class="input_text" id="b">  <?php            include 'config/config.php';  $sql="select * department deptcode=$dpttitle"; $result=mysql_query($sql);$options=""; while ($row=mysql_fetch_array($result)){     $did=$row["deptcode"]; $depts=$row["dept"]; $options.="<option value='$did'>".$depts;}?> <option value="0">select...</option> <?php echo $options; ?> </option> </select>  <script type="text/javascript"> a.onblur = function() {  b.value = this.value;}; </script> 

you're looking "ajax" functionality. try looking $.get(url,data,success); using jquery

first, remove dropbox b , replace div id="b"

$("#a").change(loadcities); function loadcities(e){     // prepare statement     var url = "http://www.yoursite.com/ajax/getcities";     var data = {         country : $("#a").val()     };     $.get(url, data, loadcitiescomplete); } function loadcitiescomplete(data){     $("#b").html(data); } 

that url : "http://www.yoursite.com/ajax/getcities" php should like

<? if(isset($_get['country'])){     $html = '<select name="b" class="input_text" id="b">';     include 'config/config.php';      $dpttitle = mysql_real_escape_string($_get['country']);     $sql="select * department deptcode=$dpttitle";     $result=mysql_query($sql);$options="";     while ($row=mysql_fetch_array($result)){         $did=$row["deptcode"];     $depts=$row["dept"];     $html .="<option value='$did'>".$depts;}?>     $html .= '<option value="0">select...</option>';     $html .= '</option>';     $html .= '</select>';     echo $html; } ?> 

obviously php should use country $_get var properly, use pdo or mysqli prepared statements safety, etc. this'll moving in right direction.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -