我如何动态填充下拉菜单取决于一些其他下拉菜单


How can i dynamically fill drop down menu depending on some other drop down menu?

我有两个下拉菜单。首先,您选择国家,其次,您应该能够选择城市。城市应该根据所选择的国家而有所不同。国家和城市都是从数据库中加载的。

数据库中有两个表,包含以下行:

  • 国家
    • countryId
    • <
    • 名称/gh>
  • 城市
    • cityId
    • <
    • 名称/gh>
    • countryId

这是索引:

<html>
<head></head>
<body>
    <select>
        <?php
            $con=mysqli_connect("localhost","root","","database");
            $result = mysqli_query($con,"SELECT * FROM countries");
            while($row = mysqli_fetch_array($result)) {
                echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
            }
        ?>
    </select>
    <br />
    <select>
        <!-- Here goes select from countries -->
    </select>
</body>

如何填充二次选择?

给第一个选择框命名为firstrongelect第二个选择框命名为second_select

希望这对你有帮助

<script type="text/javascript">
$(document).ready(function() {
    $("#first_select").change(function() {
        $.get('getcities.php?first_select=' + $(this).val(), function(data) {
            $("#second_select").html(data);
        }); 
    });
});
</script>
<form  method="get">
<select name="first_select" id="first_select">
       <?php
            $con=mysqli_connect("localhost","root","","database") or die(mysqli_error());
            $result = mysqli_query($con,"SELECT * FROM countries") or die(mysqli_error());
            while($row = mysqli_fetch_array($result)) {
                echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
            }
        ?>
    </select>
    <select name="second_select" id="second_select"></select>
</form>

创建一个名为getcities.php和

的文件在<

strong> getcities.php

$first_select= $_GET['first_select'];
$query = mysql_query("select * from cities where countryid = {$first_select}");
while($row = mysql_fetch_array($query)) {
    echo "<option value='$row[cityid]'>$row[name]</option>";
}

你需要使用一些条件逻辑,为他们为他们的国家所做的选择分配一个变量,例如"countryChoice",然后在你的第二个下拉列表中,使用另一个while语句列出所有具有countryChoice的城市。

像这样:

<html>
<head></head>
<body>
    <form method="post" action="/*YOUR ACTION */">
    <select name='countryChoice' id='countryChoice'>
        <?php
            $con=mysqli_connect("localhost","root","","database");
            $result = mysqli_query($con,"SELECT * FROM countries");
            while($row = mysqli_fetch_array($result)) {
                echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
            }
        ?>
    </select>
    <br />
    <select name='cityChoice' id='cityChoice'>
        <?php
            $countryChoice = $_POST['countryChoice'];
            $con=mysqli_connect("localhost","root","","database");
            $result = mysqli_query($con,"SELECT * FROM cities WHERE countryId='$countryChoice'");
            while($row = mysqli_fetch_array($result)) {
                echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
            }
        ?>
    </select>
    </form>

你可能需要稍微改变一下,我有一段时间没有使用这个了。但是,我希望这能给你正确的想法。

或者,如上所述,您可以使用Ajax。