【问题标题】:how to load a drop down with a query using the result from a previous drop down如何使用上一个下拉列表的结果通过查询加载下拉列表
【发布时间】:2017-06-07 03:31:56
【问题描述】:

这是我 PHP 和 HTML 职业生涯的早期。我想用 SQL 查询填充一个 - 没问题。但是,从该查询中获取突出显示的值并使用它来驱动另一个查询,以填充不同的下拉列表。

我不明白值是如何在 HTML 和 PHP 之间传递的,我应该使用全局变量来存储第一个下拉列表中的选择吗?我该怎么做?

除了使用按钮来指示第一个下拉列表中当前突出显示的值将用于驱动填充第二个下拉列表的查询之外,还有其他选择吗?

我的代码如下所示:

 Asset type:<br/>
     <select size=3 class="element select medium" id="WantAssetCategory" name="WantAsset Class"> 
        <?
        $sql = "SELECT * from Categories";

        $result = $dbcon->query($sql);
        foreach ($result as $row){
            $catname = $row['description'];
            $catx    = $row['id'];
            echo '<option value = ' . "$catx> $catname</option>" ;
        }
        $result = null;
        ?>                    
    </select>

【问题讨论】:

  • 您可以使用 Ajax 在更改第一个选择下拉菜单时运行并获取所选值,然后使用该值运行查询。 ajax 调用成功后,创建新的选择下拉列表,其中预先填充了查询的结果,并用它替换第二个下拉 html。希望对您有所帮助,如果您需要的话,我会尝试为它编写一个演示代码。

标签: php html sqlite


【解决方案1】:
<select size=3 class="element select medium" id="WantAssetCategory" name="WantAsset Class">

使用 jQuery 监听 select 的变化并使用选定的值执行操作脚本,作为响应,使用提到的 id 更新新下拉列表的选项。

document.getElementById("WantAssetCategory").onchange = function() {
   $.ajax({
                data: this.value, // This is the selected value from your drop down.
                type: "POST", // GET or POST
                url: "query.php", // the file(where the query is) to call
                success: function(response) {
                    $('#output').html(response);// repose response should contain the new select options and #output should be the id of the new select drop down.
                }
            });
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    相关资源
    最近更新 更多