gpt4 book ai didi

javascript - select col2 from TABLE 其中 col1 值是从多个选择下拉列表中选择的

转载 作者:行者123 更新时间:2023-11-29 19:18:46 25 4
gpt4 key购买 nike

这是我的关键字表

mainCat    | subCat1
-----------+-------------
Technology | Electronics
Technology | Computers
Technology | Nano
Health | Diet
Health | Fitness
Health | Exercise

下面是带有两个选择框的代码。

当我从第一个下拉列表中选择技术时,第二个下拉列表将更新为电子、计算机、纳米。但我现在的要求是从第一个下拉列表中选择多个值。假设我选择了技术、健康,第二个下拉列表必须更新为电子、计算机、纳米、饮食、健身、锻炼

<html>
<?php
require_once 'dbconnect.php';
$result = mysql_query("SELECT DISTINCT mainCat FROM keyword");
?>

<head>
<link rel="stylesheet" href="assets/css/bootstrap.min.css" type="text/css"/>
<script src="assets/jquery-1.11.3-jquery.min.js"></script>
<script> <!-- SCRIPT TO UPDATE SUB CAT_1 DROPDOWN -->
function showUser(str) {
if (str == "") {
document.getElementById("e7").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("e7").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getsub1.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form method="post" action="">
Main Category Keyword : <select multiple="" name="e6" id="e6" style="width: 300px; display: none;" onchange="showUser(this.value)" class="populate" tabindex="-1">
<option value="">Select Main Category</option>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['mainCat'] . "'>" . $row['mainCat'] . "</option>";
}
?>
</select>
</br>
Sub Category Keyword : <select name="e7" multiple="" id="e7" style="width: 300px; display: none;" class="populate" tabindex="-1">
<option value="">Select Sub Category</option>
</select>
</br>
<input type="submit" name="submit" value="Add">
</form>
</body>
</html>

这是我的 getsub1.php

<html>
<head>
</head>
<body>
<?php
require_once 'dbconnect.php';
$q = $_GET['q'];
echo"<option value=''>Select Sub Category</option>";
$sql1="SELECT DISTINCT subCat1 FROM keyword WHERE mainCat = '".$q."'";
$result1 = mysql_query($sql1);
while ($row = mysql_fetch_array($result1)) {
echo "<option value='" . $row['subCat1'] . "'>" . $row['subCat1'] . " </option>";
}
?>
</body>
</html>

关于我可以更改哪些内容以使其正常工作有什么建议吗?

最佳答案

首先更改您的表,因为它不是按照 sql 标准制作的。

 ID|MainCat
--+-------
1 |Technology
2 |Health



mainCat | subCat1
-----------+-------------
1 | Electronics
1 | Computers
1 | Nano
2 | Diet
2 | Fitness
2 | Exercise

然后您可以添加过滤器数组,当用户检查一个主要类别时,将该索引添加到数组中:

$filters;
$filters[0] = 1;

//用户选择另一个

$filters[1] = 2;

之后您可以创建搜索查询:(伪代码)

    //$search_string = "SELECT subcat FROM subcatsTable where ";

//foreach filter in filters
//$search_string+= "maincat="+filter
//$search_string+= " OR "
//$result = mysql_query($search_string);

关于javascript - select col2 from TABLE 其中 col1 值是从多个选择下拉列表中选择的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com