我有一個MySQL表 科目(subject_id,faculty_id)動態更新下拉
我有一個下拉,從該表中填充subject_id。 當用戶選擇subject_id「1」想要一個sql查詢來從同一個表中選擇所有faculty_id並更新第二個下拉列表,其中subject_id =「selected drop down subject_id」
我該怎麼做?
謝謝,
我有一個MySQL表 科目(subject_id,faculty_id)動態更新下拉
我有一個下拉,從該表中填充subject_id。 當用戶選擇subject_id「1」想要一個sql查詢來從同一個表中選擇所有faculty_id並更新第二個下拉列表,其中subject_id =「selected drop down subject_id」
我該怎麼做?
謝謝,
您是否試圖複製此模型? =>http://www.plus2net.com/javascript_tutorial/dropdown-list-demo.php
如果是的話,解決的辦法是在這裏=>http://www.plus2net.com/javascript_tutorial/dropdown-code.php
谷歌是您的朋友:2下拉列表mysql的更新。第二個結果是資源
這就是我正在尋找,除了在這種情況下數據被硬編碼到HTML,IM從mysql表中填充兩個數據 – ABI
這樣做的最好方法是使用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>
<?
}
?>
我沒有測試它,但它的想法。
你應該使用ajax。嘗試查看'jQuery'或'ExtJS'框架,它們可以對你有用 – k102
關於我們要討論多少個主題?對於少量的數據,你可以在JS內部完成。 – ty812
我有很多datat存儲在mysql中需要填充到下拉,所以我不能使用js或硬編碼到html – ABI