如何在第一个下拉列表中填充第二个下拉框(硬)

时间:2015-05-29 09:07:25

标签: javascript php jquery html mysql

我想根据用户在第一个选择的值填充我的第二个下拉框。这是我到目前为止所做的:

在PHP文件中:

function displayDropDown()
{
    $table_tester = "TBL_TESTER_LIST";
    $query_string = "select * from $table_tester";
    $result = @mysql_query($query_string) or die (mysql_error());

    echo "<select id=\"tester_type\" onChange=\"getTesterType();\">";
    while($data = mysql_fetch_array($result))
    {
        $tester_type = $data['tester_type'];
        echo "<option value='$tester_type'>$tester_type</option>"; // first drop down
    }
    echo "</select>";
}

function displaySecondDropDown($selected_tester_type)
{
    $table_tester = "TBL_TESTER_LIST";
    $query_string = "select * from $table_tester where tester_type = '$selected_tester_type'";
    $result = @mysql_query($query_string) or die (mysql_error());

    echo "<select>";
    while($data = mysql_fetch_array($result))
    {
        $tester_name = $data['tester_name'];
        echo "<option value='$tester_name'>$tester_name</option>";// second drop down
    }
    echo "</select>";
}

?>
<?php

$action = rtrim($_REQUEST['action']);

if($action=="insert")
{
    $tester_type = rtrim($_REQUEST['tester_type']);
    $tester_name  = rtrim($_REQUEST['tester_name']);

    echo checkDuplicateTesterName($tester_type, $tester_name);
}
else if($action=="displayDropDown")
{
    $selected_tester_type = $_REQUEST['tester_type'];

    echo displayDropDown();
    echo displaySecondDropDown($selected_tester_type);
}

?>

在外部JavaScript文件中:

function displayDropDown()
{
    var page = "database.php";

    $.post(page, {
        action : "displayDropDown"
    }, function(data) {
        $("div#drop_two_list").html(data);
    });
}

function getTesterType()
{
    var tester_type = $("#tester_type").val();
    var page = "database.php";

    $.post(page, {
        tester_type : tester_type
        }, function(data) {
        $("div#drop_two_list").html(data);
    });
}

在HTML文件中:

<html>
    <head>
        <title>Delete Tester</title>
        <link rel="stylesheet" type="text/css" href="style.css"/>
        <script language="javascript" type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="function.js"></script>
        <script type="text/javascript">displayDropDown();</script>
    </head>

    <body>
        <h3>Delete Tester</h3>
        <div id="drop_two_list"></div>
        <table class="deleteTable">
            <tr>
                <td/><td><br>
                    <input type="button" value="Cancel" onclick="window.close();"/>
                    <input type="button" name="send" value="Delete" onclick="deleteTester();"/>
                </td>
            </tr>
    </table>
    </body>
</html>

确实会出现两个下拉框。第一个有值(这是错误的我将在后面的部分解释),第二个是空的。此外,出现错误:Notice: Undefined index: tester_type in /opt/lampp/htdocs/Signup/module1/database.php on line 86在PHP文件中为$selected_tester_type = $_REQUEST['tester_type'];。如果我要在第一个中选择一个值,则另一个错误会替换页面上的所有内容:Notice: Undefined index: action in /opt/lampp/htdocs/Signup/module1/database.php on line 75 $action = rtrim($_REQUEST['action']);

现在,在我说明为什么第一个下拉框中有错误的值的部分。在继续之前,为了更好地理解我的问题,我想要填充下拉框的表格(TBL_TESTER_LIST)。 注意:我只是按随机顺序显示5个样本行,因为我在此表中有超过 500行,并且不想将所有内容放在此处。

 id      tester_type   tester_name
 1         LMX           LMX-01
 2         LMX           LMX-04
 26        LCX           LCX-06
 40        CAT           CAT-14
 95        HPPS          HPPS-01

我想在第一个下拉框中填充tester_type列,并且test_name列是我想要填充第二个下拉框的相应内容。正如你所看到的,test_type有重复项,因为一个tester_name可以属于同一个tester_type(一个例子是green apple no.1(tester_name)是一个apple(tester_type)而green apple no.2(tester_name)也是一个apple (tester_type))。

我目前在第一个下拉框中得到的只是:LMX,LMX,LMX,LMX等。那是因为我的前31行是LMX。如何在第一个下拉框中编码,以便它只显示每种类型中的一个,而我的第二个下拉框将显示基于用户选择的值的值?

示例如下:如果用户在第一个下拉框中选择LMX,则所有LMX tester_type的tester_names将填充第二个下拉框。

如果需要更多信息,请告诉我。一直在寻找解决方案5天,我似乎还没有得出结论。

1 个答案:

答案 0 :(得分:0)

如果没有问为什么你的表是这样构造的,那么在查询中使用DISTINCT子句来消除重复是不够的。

select DISTINCT tester_type from $table_tester