2011-08-26 61 views
0

我有一個MySQL表 科目(subject_id,faculty_id)動態更新下拉

我有一個下拉,從該表中填充subject_id。 當用戶選擇subject_id「1」想要一個sql查詢來從同一個表中選擇所有faculty_id並更新第二個下拉列表,其中subject_id =「selected drop down subject_id」

我該怎麼做?

謝謝,

+1

你應該使用ajax。嘗試查看'jQuery'或'ExtJS'框架,它們可以對你有用 – k102

+0

關於我們要討論多少個主題?對於少量的數據,你可以在JS內部完成。 – ty812

+0

我有很多datat存儲在mysql中需要填充到下拉,所以我不能使用js或硬編碼到html – ABI

回答

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> 

而且用於檢索DATAS文件(注意:這裏沒有HTML,頭部或身體標記):

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> 
<? 
    } 
?> 

我沒有測試它,但它的想法。