?>
<?php
$get1 = mysqli_query($conn, "SELECT * FROM category ORDER BY categoryid
ASC");
$option1 = '';
while ($row1 = mysqli_fetch_array($get1, MYSQLI_ASSOC)) {
$option1 .= '<option value = "' . $row1['categoryname'] . '">' .
$row1['categoryname'] . '</option>';
}
if (isset($_POST['categoryname'])) {
$get3 = mysqli_query($conn, "select subcategoryname from subcategory where
categoryid = (select categoryid from category where categoryname
='$_POST[categoryname]' )");
$option3 = '';
while ($row3 = mysqli_fetch_array($get3, MYSQLI_ASSOC)) {
$option3 .= '<option value = "' . $row3['subcategoryname'] . '">' .
$row3['subcategoryname'] . '</option>';
}
}
?>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label style="margin-left:6px;margin-bottom: 30px; ">Category:</label>
<select onchange='this.form.submit()' selected name = "categoryname"
style="margin-left: 70px;width: 200px">
<?php echo $option1; ?>
</select><noscript><input type="" ></noscript> <br> <br>
<label style="margin-left:6px;margin-bottom: 30px; ">Sub
Category:</label><select name = "subcategoryname" style="margin-left:
70px;width: 200px">
<?php echo $option3; ?>
</select> <br> <br>
</form>
马先生,先生,我需要帮助。在我选择类别而不返回其默认值或第一个类别后,我将询问我将如何生成子类别选择框,因为如果我点击类别中的任何一个,它将在子类别框中生成数据但在此之后它将影响类别将恢复其默认值。所以我想要的仍然会保留我选择的类别,并仍然会生成子类别,我希望有人可以帮助我..提前感谢。
答案 0 :(得分:0)
为什么你不尝试使用jquery和ajax先生?你想要触发类别吧?你只需要从类别中选择onchange然后将其传递给带有返回值的选项子类别,返回值可以解决你的问题,例如:你的选项类别中的onchange =“return.this.value”。 这个例子可以帮助你:http://suckittrees.com/artikel-209/database-desa-kecamatan-kabupatenkota-dan-provinsi-se-indonesia--program-ajax.html