2015-05-29 170 views
0

我想根据用户在第一个框中选择的值来填充第二个下拉框。这是我迄今所做的:如何填充第一个下拉框中的第二个下拉框(硬)

在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列是我想填充我的第一个下拉框中什么,tester_name列是我想填充我的第二个下拉框中,相应的内容。正如你所看到的那样,tester_type是重复的,因为一个tester_name可以属于同一个tester_type(一个例子是绿色苹果no.1(tester_name)是苹果(tester_type),绿色苹果no.2(tester_name)也是苹果(tester_type))。

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

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

如果桌面上需要更多信息,请告诉我。一直在寻找解决方案5天,我似乎并没有接近结论。

+0

在这两个JS函数中,您都需要传递'action'和'tester_type'。 对于'displayDropDown'函数,'action'的值为“displayDropDown”,'tester_type'为“”。 在第二个函数中。把'action'放在值“displayDropDown”中。 希望它有帮助。 –

+0

嗯,这可能是其中一个错误,但我仍然遇到同样的问题。 – hzq

回答

0

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

select DISTINCT tester_type from $table_tester 
相关问题