如何从父选择PHP填充子下拉菜单

时间:2014-07-21 14:19:35

标签: php mysql forms validation drop-down-menu

如何使用PHP从父下拉列表中填充子项下的静态页面上的选择。例如,在您选择了您所居住的州之后,或者在我的情况下,在制造商之后填充系列文件时,让孩子下降填充县。

编辑:有没有办法在没有js的情况下做到这一点?

<?php
$man = mysqli_query($con,"SELECT DISTINCT manufacturer FROM inventory.manufacturer WHERE manufacturer!=\"\" ORDER BY manufacturer;");               
echo "<select name=\"manufacturerS\">
<option value=\"\">Select</option>";
while($row = mysqli_fetch_array($man)) {
    echo "<option value=\"".$row['manufacturer']."\">".$row['manufacturer']."</option>";
}   
echo "</td>
<td>";

if(isset($_POST['manufacturerS'])){
$ser = mysqli_query($con,"SELECT DISTINCT series, manufacturer FROM inventory.audit WHERE series!=\"\" AND manufacturer='".$_POST['manufacturerS']."' ORDER BY series;");               
echo "<select name=\"seriesS\">
<option value=\"\">Select</option>";
while($row = mysqli_fetch_array($ser)) {
    echo "<option value=\"".$row['series']."\">".$row['series']."</option>";
}   
    echo "</td>
    <td>";      
}

1 个答案:

答案 0 :(得分:0)

您需要客户端来确定何时选择了manufacturerS select,然后您可以使用纯javascript

<form id="manufacturerF" method="POST">
<select name="manufacturerS" onchange="document.getElementById('manufacturerF').submit();">
<?php  
    $man = mysqli_query($con, "SELECT DISTINCT manufacturer 
    FROM inventory.manufacturer 
    WHERE manufacturer!=\"\" 
    ORDER BY manufacturer;");

    while ($row = mysqli_fetch_array($man)) {
        echo '<option value="'.$row['manufacturer'].'>'.$row['manufacturer'].'</option>';
    } 
?>
</select>
</form>


<?php
    if(isset($_POST['manufacturerS']) && !empty($_POST['manufacturerS'])){  
        echo '<select name="seriesS">';

        $ser = mysqli_query($con,"SELECT DISTINCT series, manufacturer 
        FROM inventory.audit 
        WHERE series!=\"\" 
        AND manufacturer='".$_POST['manufacturerS']."' 
        ORDER BY series;");

        while ($row = mysqli_fetch_array($ser)) {
            echo '<option value="'.$row['series'].'">'.$row['series'].'</option>';
        }   

        echo '</select>';   
    }
?>

我建议您使用JQuery,因此不需要<form>并重新加载您的网页。 正确的想法是使用JQUERY来检测select1何时被选中,然后用AJAX填充select2