$subject_name) -6ren">
gpt4 book ai didi

php - 在选择组合框值时,必须在从数据库中选择查询后动态显示标签

转载 作者:行者123 更新时间:2023-11-29 23:19:48 25 4
gpt4 key购买 nike

在文件中编写以下代码来填充组合框的值:

<select id="company" required="required" class="form-control" value = "select"    name="subject_code" placeholder="Select" >
<?php
//Iterating list of subjects available to be filled .
echo "<option> Select </option>";
foreach ($subjects_to_fill as $subject_id => $subject_name) {
# code...
echo "<option value=".$subject_id."> ".$subject_name." </option>";

}
?>
</select>

从组合框中选择特定项目时,我希望根据$subject_idfaculty_table动态显示faculty_name。

表结构:

faculty_table(faculty_id,faculty_name,subject_id)
subject_table(subject_id,subject_name,faculty_id)

最佳答案

您需要使用 ajax 来完成此任务。

添加<div>以及当前文件中的以下脚本。

<div id="faculty"></div>
<script type="text/javascript">
function get_faculty()
{
var id = document.getElementById("company").value;
var dataString = 'id='+ id;
$.ajax
({
type: "POST",
url: "get_faculty.php",
data: dataString,
success: function(html)
{
$("#faculty").html(html);
}
});
}

</script>

现在在同一文件夹中创建另一个 get_faculty.php 文件,该文件将在公司下拉列表的 onchange 事件上调用。

在该文件中写入以下代码

    if($_POST['id'])
{
$id=$_POST['id'];

$con=mysqli_connect("localhost","username","pass","dbname");
$sql=mysqli_query($con,"SELECT faculty_name from faculty_table where subject_id = ".$id);
while($row=mysqli_fetch_array($sql))
{
echo $row['faculty_name'];

}
}

不要忘记写入 mysqli_connect 凭据并进行相应的查询。

关于php - 在选择组合框值时,必须在从数据库中选择查询后动态显示标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420761/

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