【发布时间】:2011-12-06 02:31:37
【问题描述】:
我对这个 jquery ajax 很陌生。我想知道的是如何根据第一个选择框的选择填充第二个选择框的内容。说这里是我的 HTML:
<form method="post" action="tosomewhere.php">
<table>
<tbody>
<tr>
<td>
<select id="first" name="year">
<option value="1">1</option>
<option value="2">2</option>
</select>
</td>
</tr>
<tr>
<td>
<select id="second" name="section">
</select>
</td>
</tr>
</tbody>
</table>
</form>
脚本:
<script type="text/javascript">
$(function () {
$("#first").change(function () {
$("#second").load('second_option.php?year=', {first: $(this).val()});
});
});
</script>
PHP 文件:
<?php
require '../../config/dbconfig.php';
$year = $_GET['year'];
$sql = "SELECT * FROM section_tb WHERE year = ?";
$stmt = $db->prepare($sql);
$stmt->execute(array($year));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$second_option = "";
foreach($result as $key => $value)
$second_option .= "<option value='".$value['section_name']."'>".$value['section_name']."</option>";
echo $second_option;
?>
我的第二个选择框的内容将从第一个选择框的 WHERE 值的 mysql 结果中填充。我可以构建 PHP 文件,我只是对如何在选择中返回和填充它感到困惑。
任何教程或示例的线索?非常感谢。
编辑:添加了 jquery 脚本,但仍然没有运气。我试图 var_dump($second_option) 它有内容。
【问题讨论】: