doubei8541 2018-10-29 19:39
浏览 62

使用同一表格上另一个组合框的结果过滤组合框

I have been searching online for a solution and none of them seem to work, I am trying to filter the options in a combobox which is supposed populated using an sql query that filters uding the entry of another combobox.

I have been able to populate the combobox from the sql database using php but I cant filter the results using the first combobox.

My coding is below;

Query code

<?php
$connection = mysqli_connect("", "", "", "");

$lab = mysqli_real_escape_string($_POST['LabDep']);
$results = mysqli_query($connection, "SELECT SampleID, SampleType FROM `sampletypes` WHERE Lab = $lab");

mysqli_close($connection);
?>

Combobox code

<select input name="SampType1">
    <?php foreach($results as $user): ?>
        <option value="<?= $user['SampleID']; ?>"><?= $user['SampleType']; ?></option>
    <?php endforeach; ?>
</select style="visibility:hidden;"/>

AJAX code made from I don't know how many online guides so probably a real hash as I haven't used this before

<script>
function GetNames(LabDep) {
    if (genderID > 0) {
        $("#SampType1").get(0).options.length = 0;
        $("#SampType1").get(0).options[0] = new Option("Loading samples", "-1"); 

        $.ajax({
            type: "POST",
            url: "",
            data: "{Lab:" + Lab + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                $("#SampType1").get(0).options.length = 0;
                $("#SampType1").get(0).options[0] = new Option("Select name", "-1"); 

                $.each(msg.d, function(index, item) {
                    $("#SampType1").get(0).options[$("#SampType1").get(0).options.length] = new Option(item.Display, item.Value);
                });
            },
            error: function() {
                $("#SampType1").get(0).options.length = 0;
                alert("Failed to load names");
            }
        });
    }
    else {
        $("#SampType1").get(0).options.length = 0;
    }
}
</script>

there will end up being 4 comboboxes that will need updating, sampletype1, sampletype2, sampletype3 and sampletype4, this will also only be visible when certain quantities are selected (already coded and working)

It is so frustratingly close to working, I just dont know where I am going wrong.

Thank you all in advance for your help.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名
    • ¥65 汇编语言除法溢出问题