如何根据其他下拉菜单动态填充下拉菜单?

时间:2014-03-23 14:52:26

标签: php html html-select

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

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

  • 国家
    • countryId
    • 名称
  • 城市
    • cityId
    • 名称
    • 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>

如何填写第二个选择?

2 个答案:

答案 0 :(得分:0)

在您的第一个选择中添加一些名称,让我们在此处将其称为first_select,并将第二个选择框称为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和

的文件 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>";
}

答案 1 :(得分:-1)

你需要使用一些条件逻辑,为他们为国家选择的选择分配一个变量,即“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。