我有两个下拉列表,如果一个值选择另一个值,则依赖于其他值,从数据库加载相同的值。例如,如果我选择一个国家/地区,则加载与该国家/地区相同的城市。
<select name="A" class="input_text" id="A">
<?php
include 'config/config.php';
$sql="SELECT * FROM department ORDER BY Dept ASC";
$result=mysql_query($sql);$options="";
while ($row=mysql_fetch_array($result)){
$did=$row["DeptCode"];
$depts=$row["Dept"];
$options.="<OPTION value='$did'>".$depts;}?>
<option value="0">Select...</option>
<?php echo $options; ?>'
</option>
</select>
<select name="B" class="input_text" id="B">
<?php
include 'config/config.php';
$sql="SELECT * FROM department WHERE DeptCode=$dpttitle";
$result=mysql_query($sql);$options="";
while ($row=mysql_fetch_array($result)){
$did=$row["DeptCode"];
$depts=$row["Dept"];
$options.="<OPTION value='$did'>".$depts;}?>
<option value="0">Select...</option>
<?php echo $options; ?>
</option>
</select>
<script type="text/javascript">
A.onblur = function() {
B.value = this.value;};
</script>
答案 0 :(得分:1)
您正在寻找“ajax”功能。尝试使用JQuery
查看$.get(url,data,success);
首先,删除Dropbox B并将其替换为带有id="B"
$("#A").change(loadCities);
function loadCities(e){
// prepare get statement
var url = "http://www.yoursite.com/ajax/getCities";
var data = {
country : $("#A").val()
};
$.get(url, data, loadCitiesComplete);
}
function loadCitiesComplete(data){
$("#B").html(data);
}
那个网址:"http://www.yoursite.com/ajax/getCities"
php应该看起来像
<?
if(isset($_GET['country'])){
$html = '<select name="B" class="input_text" id="B">';
include 'config/config.php';
$dpttitle = mysql_real_escape_string($_GET['country']);
$sql="SELECT * FROM department WHERE DeptCode=$dpttitle";
$result=mysql_query($sql);$options="";
while ($row=mysql_fetch_array($result)){
$did=$row["DeptCode"];
$depts=$row["Dept"];
$html .="<OPTION value='$did'>".$depts;}?>
$html .= '<option value="0">Select...</option>';
$html .= '</option>';
$html .= '</select>';
echo $html;
}
?>
显然,php应该正确地使用国家$ _GET var,使用PDO或MySQLi准备好安全语句等等。但希望这会让你朝着正确的方向前进。