如果您希望在不重新加载页面的情况下无缝高效地完成此操作,您需要查看 Jquery Ajax 函数。Ajax 的工作方式是当有人在第一个框中进行选择时,它将将该数据发送到一个 php 脚本,该脚本可以从第一个框中获取答案,运行 mysql 查询,然后将新的子类别返回到原始页面而无需重新加载页面。
示例:在您的 test.php 中
//On selection change state, call the ajax
$("#elementid").change(function() {
var selection = $(this).children("option:selected");
$.ajax({
url: 'caller.php',
dataType: 'json',
data: 'selected='+selection,
success: function(data) {
//Fill the second selection with the returned mysql data
}
});
}
<select id="elementid">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
在你的 caller.php
$selection = $_POST["selected"];
//Create an array to hold all the subcategories, say the array is called $sub
echo json_encode(array(success => $sub));
exit;
请阅读Jquery.ajax