[英]Using a mysql data base to populate a second drop down box automatically based off of first drop down box
[英]How to populate second drop down box on first drop down (Hard)
我想根据用户在第一个下拉菜单上选择的值来填充第二个下拉框。 到目前为止,这是我所做的:
在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
是$selected_tester_type = $_REQUEST['tester_type'];
在PHP文件中。 如果我要在第一个中选择一个值,则另一个错误将用以下内容替换页面上的所有内容: Notice: Undefined index: action in /opt/lampp/htdocs/Signup/module1/database.php on line 75
$action = rtrim($_REQUEST['action']);
是$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(例如绿色的苹果1号(tester_name)是一个苹果(tester_type)和绿色的2号苹果(tester_name)也是一个苹果) (tester_type))。
我当前在第一个下拉框中看到的是:LMX,LMX,LMX,LMX等。 那是因为我的前31行是LMX。 我该如何编码我的第一个下拉框,使其仅显示每种类型中的一种,而我的第二个下拉框将显示基于用户选择的值?
例如:如果用户在第一个下拉框中选择LMX,则所有LMX tester_type的tester_name都将填充第二个下拉框。
如果需要桌上的更多信息,请告诉我。 在寻找解决方案已有5天了,我似乎还没有得出一个结论。
不问您为什么要以这种方式构造表,仅在查询中使用DISTINCT子句来消除重复项是不够的。
select DISTINCT tester_type from $table_tester
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.