gpt4 book ai didi

PHP、MySql 下拉列表每次选择只显示一条记录

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:23 35 4
gpt4 key购买 nike

我是这方面的新手,我正在尝试创建一个从数据库中检索数据的动态下拉列表。问题是,对于第一个下拉菜单中的每个选择,它只给我一个下拉菜单项(在第二个列表中)。有人请帮助。这是代码。

 <?php
require_once("dbcontroller.php");

$query ="SELECT * FROM campus";

?>
<html>
<head>
<TITLE>Campus and Faculty Select</TITLE>
<head>
<style>
body{width:610px;}
.frmDronpDown {border: 1px solid #F0F0F0;background-color:#C8EEFD;margin: 2px 0px;padding:40px;}
.demoInputBox {padding: 10px;border: #F0F0F0 1px solid;border-radius: 4px;background-color: #FFF;width: 50%;}
.row{padding-bottom:15px;}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>
function getcampus_id(val) {
$.ajax({
type: "POST",
url: "get_faculty.php",
data:'campus_id='+val,
success: function(data){
$("#faculty-list").html(data);
}
});
}

function selectcampus_id(val) {
$("#search-box").val(val);
$("#suggesstion-box").hide();
}
</script>
</head>
<body>
<div class="frmDronpDown">
<div class="row">
<label>Country:</label><br/>
<select name="campus" id="campus-list" class="demoInputBox" onChange="getcampus_id(this.value);">
<option value="">Select Country</option>
<?php
$query ="SELECT * FROM campus";
$results = mysqli_query($con, $query);
//loop
foreach ($results as $campus){
?>
<option value="<?php echo $campus["campus_id"]; ?>"> <?php echo $campus["name"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>State:</label><br/>
<select name="faculty" id="faculty-list" class="demoInputBox">
<option value="">Select State</option>
</select>
</div>
</div>
</body>
</html>

get_faculty.php

    <?php
require_once("dbcontroller.php");

if(!empty($_POST["campus_id"])) {
$campus_id = $_POST["campus_id"];
$query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
$results = mysqli_query($con, $query);
?>
<option value="">Select Campus</option>
<?php
foreach($results as $faculty) {
?>
<option value="<?php echo $faculty["faculty_id"]; ?>"><?php echo $faculty["faculty_name"]; ?></option>
<?php
}
}
?>

和 dbcontroller.php

<?php
$username = "root";
$password = "";
$host = "localhost";
$dbname = "registration";

$con = mysqli_connect($host, $username, $password) or die("Could not Connect");
mysqli_select_db($con, $dbname);
?>

最佳答案

使用 while($row = mysqli_fetch_assoc($result) 从数据库中获取所有记录。

<?php
require_once("dbcontroller.php");

if(!empty($_POST["campus_id"])) {
$campus_id = $_POST["campus_id"];
$query ="SELECT * FROM faculty WHERE faculty_id = $campus_id";
$results = mysqli_query($con, $query);
?>
<option value="">Select Campus</option>
<?php
while($row = mysql_fetch_assoc($results)) {
?>
<option value="<?php echo $row ["faculty_id"]; ?>"><?php echo $row ["faculty_name"]; ?></option>
<?php
}
}
?>

关于PHP、MySql 下拉列表每次选择只显示一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37072546/

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