dynamically update drop down
I have a mysql table subjects(subject_id, faculty_id)
I have a drop down that populates subject_id from this table. When a user selects subject_id "1" would like a sql query to select all faculty_id from the same table and update a second drop down where subject_id="selected drop down subject_id"
how can i do this开发者_运维知识库?
Thanks,
Are you trying to replicate this model? => http://www.plus2net.com/javascript_tutorial/dropdown-list-demo.php
If so, the solution is here => http://www.plus2net.com/javascript_tutorial/dropdown-code.php
Google is your friend : 2 dropdown list mysql update. The second result is the ressource
The best way to do this is to use ajax. This is a very simple way to do it, using 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>
And the file used to retrieve datas (note : no html, head or body tags here) :
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>
<?
}
?>
I didn't test it but it's the idea.
精彩评论