动态更新下拉列表

时间:2011-08-26 08:37:53

标签: php mysql drop-down-menu

我有一个mysql表 科目(subject_id,faculty_id)

我有一个下拉列表,填充此表中的subject_id。 当用户选择subject_id时,“1”想要sql查询从同一个表中选择所有faculty_id并更新第二个下拉,其中subject_id =“selected down down subject_id”

我该怎么做?

谢谢,

2 个答案:

答案 0 :(得分:1)

您是否尝试复制此模型? => http://www.plus2net.com/javascript_tutorial/dropdown-list-demo.php

如果是,那么解决方案在这里=> http://www.plus2net.com/javascript_tutorial/dropdown-code.php

谷歌是你的朋友:2下拉列表mysql更新。第二个结果是资源

答案 1 :(得分:0)

执行此操作的最佳方法是使用ajax。这是一个非常简单的方法,使用jQuery:

<强>的index.php

   <html>
        <head>
             <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
             <!-- here come the following of your page... -->
        </head>
        <body>
             <!-- Your body... -->
             <select name="subject_id" id="subject_id">
             <?php
             // Saying that $list is the list of users from mysql
             foreach ($list as $user) {
             ?>
                  <option value="<?php echo $user; ?>"><?php echo $user; ?></option>
             <?php
             }
             ?>
             </select>

             <select name="sub_select" id="sub_select">
             </select>

             <script type="text/javascript">
             $(document).ready(function () {
                  $('#subject_id').change(function () {
                       $('#sub_select').load('subselect.php?id=' + $('#subject_id').val());
                  }
             }
             </script>
         </body>
    </html>

用于检索数据的文件(注意:这里没有html,head或body标签):

<强> subselect.php

<?php
    $id = (int) $_GET['id'];

    // This is where you do the where subject_id="selected drop down subject_id" sql query
    $list = my_function_to_get_datas($id);
    foreach ($list as $option) {
?>
<option value="<?php echo $option; ?>"><?php echo $option; ?></option>
<?
    }
?>

我没有测试它,但这是主意。