選擇從表中選擇COL2這裏是我的關鍵字表哪里哪里COL1值從多個選擇下拉
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>
什麼我可以改變,使其工作有什麼建議?
https://www.sanwebe.com/2013/05/select-box-change-dependent-options-dynamically – sumit
@sumit看到你的鏈接,這是單選擇下拉。但我需要多個選擇值。 – Sashi