如何从数据库中填充DropDownList从php

时间:2017-07-15 07:12:41

标签: php mysql ajax

我创建了一个DropDownList,它包含来自数据库的类名(第10,11,12等等)。但是当我点击该类名时,第二个DropDownList无法从数据库中加载与类名相关的值。那么如何在PHP中获取第二个DropDownList中的值。

1 个答案:

答案 0 :(得分:0)

<?--This is 1st dropdown with values that I fetch from database -->

<?php
    $query = "select * from  batch_data where subscriber_reg_code = 
    '".$_SESSION['subCode']."' and 
    branch_reg_code='".$_SESSION['branchCode']."';";
    $result = mysql_query($query);                                              
    $count=mysql_num_rows($result);     
?>
    <select class="span8" name="sel_class" id="sel_class" 
    onChange="getHouseModel">
   <option value="">Select Class</option>
    <?php 
    while($rows=mysql_fetch_array($result)){
    $cls_name = $rows['class_name'];             
    ?>    
    <option value="<?php echo $cls_name; ?>"><?php echo $cls_name; ?>
</option>
    <?php } ?>
    </select>

<?-- And I am trying to fetch data from database on selection of previous 
 Dropdown option  -->
<?php
    $query = "select * from  batch_data where subscriber_reg_code = 
    '".$_SESSION['subCode']."' and class_name='".$cls_name."' and 
    branch_reg_code='".$_SESSION['branchCode']."';";
    $result = mysql_query($query);
    $count=mysql_num_rows($result);
    ?>
    <select class="span8" name="sel_batch" id="sel_batch">
    <option value="">Select Batch</option>
    <?php                                             
    while($rows=mysql_fetch_array($result)){
    $btc_name = $rows['batch_name'];             
    ?>    
     <option value="<?php echo $btc_name; ?>"><?php echo $btc_name; ?></option>
   <?php } ?>

  </select>