具有多个数组的链接选择框

Chained Select Boxes with Multiple Arrays

本文关键字:链接 选择 数组      更新时间:2023-09-26

我在页面上有两个选择框,我正在使用链式插件进行管理。第一个选择框标识要生成的报告类型(并且可以硬编码),该类型链接到第二个选择框,需要从数据库中获取动态数组。

我有两个选择框在工作,并且在第二个选择框中设置了一个与资金相关的阵列,如下所示。我现在遇到的问题是尝试为佣金实现另一个动态数组。我会在这里指出,选择框不限于两个报告选项 - 我只是从两个简单的查询开始,让事情正常工作,然后我将在此基础上进行构建。

$queries = "SELECT DISTINCT(ProductName) FROM dbFund ORDER BY ProductName ASC";
$datas = mysql_query($queries);
<div class="col-lg-6">
    <select class="form-control" name="reportType" id="reportType">
        <option value="">----Select Report----</option>
        <option value="funds">Funds</option>
        <option value="commission">Commissions</option>
    </select>
</div>
<div class="col-lg-6">
    <select class="form-control" name="reports" id="reports" onChange="reportsSelect(this)">
        <?php 
            while($fetch_options=mysql_fetch_array($datas)) { 
        ?>
        <option class="funds" id="<?php echo $fetch_options['id']; ?>" value="<?php echo $fetch_options['ProductName']; ?>">
            <?php echo $fetch_options['ProductName']; ?>
        </option>
        <?php } ?>
    </select>
</div>

我有一些额外的脚本,它将"报告"链接到"报告类型",并将两个选择框值发送到 php 文件,以生成一个适用于"资金"的输出表。我需要帮助添加选项 class="commission",这将是来自数据库表的类似查询。

如果我以错误的方式做到这一点,我将不胜感激一些关于我应该使用什么的指示。

编辑 - 链式插件

            <script>
            $(function() {
                $("#reports").chainedTo("#reportType");
            });
            function reportsSelect(str) {
                alert(str.options[str.selectedIndex].value);
                if (str == "") {
                    document.getElementById("reportHint").innerHTML = "";
                    return;
                } else {
                    if (window.XMLHttpRequest) {
                        xmlhttp = new XMLHttpRequest();
                    } else {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange = function() {
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                            document.getElementById("reportHint").innerHTML = xmlhttp.responseText;
                        }
                    }
                    var str = document.getElementById("reports").value;                     
                    var stl = document.getElementById("reportType").value;
                    xmlhttp.open("GET","process/getReport.php?q="+str+"&z="+stl,true);
                    xmlhttp.send();
                }
            }
        </script>

如何在数据表名称类中添加一列,以便您可以将其添加到mysql_fetch_array循环中,如下所示:

<select class="form-control" name="reports" id="reports" onChange="reportsSelect(this)">
    <?php 
        while($fetch_options=mysql_fetch_array($datas)) { 
    ?>
    <option class="<?php echo $fetch_options['class']; ?>" id="<?php echo $fetch_options['id']; ?>" value="<?php echo $fetch_options['ProductName']; ?>">
        <?php echo $fetch_options['ProductName']; ?>
    </option>
    <?php } ?>
</select>

在 reportType 函数中,最后使用 xmlhttp.responseText 做任何你想做的事情。

我不知道我是否解决了你的问题。如果我在某个地方错了,请告诉我。