从TABLE中选择col2,其中从多个选择下拉列表中选择col1值

时间:2017-03-02 08:10:01

标签: javascript php jquery html mysql

这是我的关键字表

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

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

当我从第一个下拉列表中选择Technology时,第二个下拉列表会更新为Electronics, Computers, Nano。 但我现在的要求是从第一个下拉列表中选择多个值。说我选择了Technology, Health,第二个下拉列表必须更新为Elecronics, Computers, Nano, Diet, Fitness, 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>

关于我可以改变什么以使其有效的任何建议?

2 个答案:

答案 0 :(得分:0)

你可以试试这个

   SELECT subCat1 FROM keyword WHERE mainCat = '".$q."' order by subCat1" ;

答案 1 :(得分:0)

首先更改你的表,因为它不是用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);