简体   繁体   English

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

[英]How to populate second drop down box on first drop down (Hard)

I want to populate my second drop down box based on what value the user has chosen on the first. 我想根据用户在第一个下拉菜单上选择的值来填充第二个下拉框。 This is what I have done so far: 到目前为止,这是我所做的:

In the PHP file: 在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);
}

?>

In the external JavaScript file: 在外部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);
    });
}

In the HTML file: 在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>

Two drop down boxes do appear. 确实会出现两个下拉框。 The first one has values(which are wrong I'll explain in the later part) and the second is empty. 第一个具有值(我将在后面的部分中解释这是错误的),第二个为空。 Also, an error appeared: Notice: Undefined index: tester_type in /opt/lampp/htdocs/Signup/module1/database.php on line 86 which is $selected_tester_type = $_REQUEST['tester_type']; 另外,还会出现一个错误: Notice: Undefined index: tester_type in /opt/lampp/htdocs/Signup/module1/database.php on line 86$selected_tester_type = $_REQUEST['tester_type']; in the PHP file. 在PHP文件中。 If I were to select a value in the first one then another error would replace everything on the page with: Notice: Undefined index: action in /opt/lampp/htdocs/Signup/module1/database.php on line 75 which is $action = rtrim($_REQUEST['action']); 如果我要在第一个中选择一个值,则另一个错误将用以下内容替换页面上的所有内容: Notice: Undefined index: action in /opt/lampp/htdocs/Signup/module1/database.php on line 75 $action = rtrim($_REQUEST['action']);$action = rtrim($_REQUEST['action']); .

Now onto the part where I state why the first drop down box has the wrong values. 现在转到我说明为什么第一个下拉框具有错误值的部分。 Before proceeding, to understand my question better here is the table(TBL_TESTER_LIST) that I am trying to populate the drop down boxes with. 在继续之前,为了更好地理解我的问题,这里是我要用来填充下拉框的表(TBL_TESTER_LIST)。 Note: I am only showing 5 sample rows in random order because I have more than 500 rows in this table and wouldn't want to put everything here. 注意:我只按随机顺序显示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

The tester_type column is what I want to populate my first drop down box with, and tester_name column is what I want to populate my second drop down box with, accordingly. tester_type列是我要填充第一个下拉框的内容,而tester_name列是我要填充第二个下拉框的内容。 As you can see, there duplicates for tester_type because one tester_name can belong to the same tester_type(An example would be green apple no.1(tester_name) is an apple(tester_type) and green apple no.2(tester_name) is also an apple(tester_type)). 如您所见,tester_type有重复项,因为一个tester_name可以属于同一个tester_type(例如绿色的苹果1号(tester_name)是一个苹果(tester_type)和绿色的2号苹果(tester_name)也是一个苹果) (tester_type))。

What I am currently getting in my first drop down box is just: LMX, LMX, LMX, LMX, and so on. 我当前在第一个下拉框中看到的是:LMX,LMX,LMX,LMX等。 That is because my first 31 Rows are LMX. 那是因为我的前31行是LMX。 How do I code my drop first down box such that it would only display one of each type, and my second drop down box would display the values that are based on what the user selected? 我该如何编码我的第一个下拉框,使其仅显示每种类型中的一种,而我的第二个下拉框将显示基于用户选择的值?

An example would be: If the user selects LMX in the first drop down box, then all the tester_names that are LMX tester_type would populate the second drop down box. 例如:如果用户在第一个下拉框中选择LMX,则所有LMX tester_type的tester_name都将填充第二个下拉框。

If more information needed on the table, please do tell me. 如果需要桌上的更多信息,请告诉我。 Been searching for a solution for 5 days and I don't seem to be close to a conclusion. 在寻找解决方案已有5天了,我似乎还没有得出一个结论。

不问您为什么要以这种方式构造表,仅在查询中使用DISTINCT子句来消除重复项是不够的。

select DISTINCT tester_type from $table_tester

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM