【问题标题】:How can i dynamically fill drop down menu depending on some other drop down menu?如何根据其他下拉菜单动态填充下拉菜单?
【发布时间】:2014-03-23 14:52:26
【问题描述】:

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

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

  • 国家
    • 国家标识
    • 姓名
  • 城市
    • 城市标识
    • 姓名
    • 国家标识

这是索引:

<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>

如何填写第二个选择?

【问题讨论】:

  • 你可以用ajax来做。
  • 请注意标签不是关键字。在标签列表中填充与您的问题相同的单词(html、动态、下拉列表)将无助于对其进行分类。请务必阅读选择标签时出现的说明!

标签: php html html-select


【解决方案1】:

给你的第一个选择起个名字,我们在这里称它为 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>";
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 因为第二个下拉列表填充了 WHERE 查询,所以它只填充第一个下拉列表选择的内容。
    • php 不会第二次执行,除非在 ajax 中
    • 在我的测试网站上工作。
    • 没有 ajax 没有人可以第二次执行 php 代码。第二个下拉菜单必须在选择第一个选择框值时更改。没有 ajax 我们不能这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2018-11-12
    • 2018-10-22
    • 2013-05-23
    • 2014-07-17
    相关资源
    最近更新 更多