如何使用上一个下拉列表中的结果使用查询加载下拉列表

时间:2017-01-22 15:09:16

标签: php html sqlite

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

1 个答案:

答案 0 :(得分:0)

<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.
                }
            });
}

我希望有所帮助。

相关问题