嗨,我在 php 中使用 mysql 填充了这个列表框
<?php
mysqli_query($con,"set character_set_server='utf8'");
mysqli_query($con,"set names 'utf8'");
$r=mysqli_query($con,"select id_livestream,name_match from Livestream");
?>
<br>تشكيلة مباراة :<select name=livestream size=1>
<?php
while ($d=mysqli_fetch_array($r,MYSQLI_BOTH))
{
?>
<option value="<?=$d[0];?>"><?=$d[1];?></option>
<?php
}
?>
</select>
如何根据第一个列表框中选定的值从 mysql 创建并填充另一个列表框?
如有任何建议,我们将不胜感激
最佳答案
假设您的第二个 dropdwon 数据将从数据库表中填充,并且与直播表存在外键关系。
我尝试过使用 Ajax,如下所示。请参阅附件。让我知道它是否适合您并接受我的答案。
DisplayDropdown.php:这是主页面,您可以在其中选择下拉列表。
<?php
$con=mysqli_connect("localhost","UserName","","DatabaseName"); // Database connection
mysqli_query($con,"set character_set_server='utf8'");
mysqli_query($con,"set names 'utf8'");
$r=mysqli_query($con,"select id_livestream,name_match from Livestream");
?>
<html>
<head>
<script>
function getSubStream(str) {
if (str == "") {
document.getElementById("DivIDSubCategory").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("DivIDSubCategory").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","GetCategory.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
Select Live Stream: <select name=livestream size=1 onchange="getSubStream(this.value)">
<option value="select">Select</option>
<?php
while ($d=mysqli_fetch_array($r,MYSQLI_BOTH))
{
?>
<option value="<?=$d[0];?>"><?=$d[1];?></option>
<?php
}
?>
</select>
<br/>
<br/>
<div id="DivIDSubCategory"></div>
</form>
</body>
</html>
GetCategory.php:这里我们根据第一个下拉列表选择创建一个新的下拉列表
<?php
$con=mysqli_connect("localhost","UserName","","DatabaseName"); // Database connection
?>
<!DOCTYPE html>
<html>
<body>
<?php
$q = intval($_GET['q']);
mysqli_query($con,"set character_set_server='utf8'");
mysqli_query($con,"set names 'utf8'");
$sql = "SELECT subID,SubStreamName FROM substreams where livestreamID = '".$q."'";
$r=mysqli_query($con,$sql);
if(mysqli_num_rows($r) > 0) {
echo "Select Sub Stream: <select name=substream><option value=Select>Select</option>";
while ($d=mysqli_fetch_array($r,MYSQLI_BOTH))
{
?>
<option value="<?=$d[0];?>"><?=$d[1];?></option>
<?php
}
echo "</select>";
}
mysqli_close($con);
?>
</body>
</html>
一些其他资源:
Using PHP & MySQL to populate dropdown
Populate another select dropdown from database based on dropdown selection
How can i populate a dropdown list by selecting the value from another dropdown list
关于php - 使用 mysql 从另一个列表框填充列表框 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43851661/