gpt4 book ai didi

php - 根据下拉选择从数据库中填充另一个选择下拉列表

转载 作者:可可西里 更新时间:2023-11-01 13:05:22 25 4
gpt4 key购买 nike

我正在建立一个网站来学习编码,并试图建立一个工具,用户点击一个选择/下拉菜单,其中包含从数据库 cat 中提取的一些类别名称,然后另一个选择将出现从数据库 subcat 中提取的子类别名称。这几乎与 Yelp 的完全一样(进入类别)like Yelp's (go down to the categories) .

我还做了一个图:

enter image description here

我已经有一个从 cat 数据库中提取的类别下拉列表:

<p><b>Category:</b><br />
<?php
$query="SELECT id,cat FROM cat";
$result = mysql_query ($query);
echo"<select name='cselect3' class='e1'><option value='0'>Please Select A Category</option>";
// printing the list box select command
while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"".htmlspecialchars($catinfo['cat'])."\">".$catinfo['cat']." </option>";

}

echo"</select>";
?>

我有一个从 subcat 数据库中提取的 subcat:

<p><b>Subcat1:</b><br />
<?php
$query="SELECT id,subcat FROM subcat";
$result = mysql_query ($query);
echo"<select name='sselect1' class='e1'><option value='0'>Please Select A Category</option>";
// printing the list box select command
while($catinfo=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=\"".htmlspecialchars($catinfo['subcat'])."\">".$catinfo['subcat']."</option>";

}

echo"</select>";
?>

如何根据用户点击的类别创建子类别下拉列表并使其自动显示?非常感谢您提供的所有帮助!

最佳答案

我只想用 php 将变量放在 javascript 中,然后使用 javascript 函数。不需要 jquery 或 AJAX。

但是,无论如何,您都需要为子类别设置一个外键。即 - 对于 subcat 表中的每条记录,您都需要为其提供一个 catid,以便进行引用...

<?php
$db = new mysqli('localhost','user','password','dbname');//set your database handler
$query = "SELECT id,cat FROM cat";
$result = $db->query($query);

while($row = $result->fetch_assoc()){
$categories[] = array("id" => $row['id'], "val" => $row['cat']);
}

$query = "SELECT id, catid, subcat FROM subcat";
$result = $db->query($query);

while($row = $result->fetch_assoc()){
$subcats[$row['catid']][] = array("id" => $row['id'], "val" => $row['subcat']);
}

$jsonCats = json_encode($categories);
$jsonSubCats = json_encode($subcats);


?>

<!docytpe html>
<html>

<head>
<script type='text/javascript'>
<?php
echo "var categories = $jsonCats; \n";
echo "var subcats = $jsonSubCats; \n";
?>
function loadCategories(){
var select = document.getElementById("categoriesSelect");
select.onchange = updateSubCats;
for(var i = 0; i < categories.length; i++){
select.options[i] = new Option(categories[i].val,categories[i].id);
}
}
function updateSubCats(){
var catSelect = this;
var catid = this.value;
var subcatSelect = document.getElementById("subcatsSelect");
subcatSelect.options.length = 0; //delete all options if any present
for(var i = 0; i < subcats[catid].length; i++){
subcatSelect.options[i] = new Option(subcats[catid][i].val,subcats[catid][i].id);
}
}
</script>

</head>

<body onload='loadCategories()'>
<select id='categoriesSelect'>
</select>

<select id='subcatsSelect'>
</select>
</body>
</html>

关于php - 根据下拉选择从数据库中填充另一个选择下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11636293/

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