gpt4 book ai didi

php - 使用 php 和 mysql 的多个下拉列表

转载 作者:行者123 更新时间:2023-11-29 20:16:43 24 4
gpt4 key购买 nike

我得到了包含多个表的数据库,例如地区/国家/州/等。我想根据所选的其他列表制作下拉列表。

我尝试了很多方法,但似乎没有任何效果。

这是我的最新版本:

核心.php:

 <html>
<body>

<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function abc(){
var val = document.getElementById('Region_ID').value;
$.post("getSecondDropDown.php",{ Region_ID:val}, function( data ) {
$("#Country_ID").html(data);

});
}
</script>


<form action="/NewService.php" id="ServiceForm" method="post">
Name:<input type="text" name="Service_Name"></br>
Region: <select name="Region_ID" onchange="abc()" form="ServiceForm">
<?php

include('config.php');

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT Region_ID, Region_Name FROM Regions");
$stmt->execute();

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

foreach($stmt as $v) {
echo "<option value='" . $v['Region_ID'] ."'>" . $v['Region_Name'] ."</option>";

}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;

?>
</select></br>


Country: <select name="Country_ID" form="ServiceForm">

</select></br>


<input type="submit">
</form>

</body>
</html>

getSecondDropDown.php:

<?php
$Region_ID =$_POST['Region_ID'];
$option="";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT Country_ID, Country_Name FROM Countries WHERE Region_ID ='$Region_ID'");
$stmt->execute();

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

foreach($stmt as $v) {
echo "<option value='" . $v['Country_ID'] ."'>" . $v['Country_Name'] ."</option>";

}
echo $option;
?>

Image

最佳答案

这是你的问题,你没有在 select 标签中写入 id 属性

<select name="Region_ID" id="Region_ID" onchange="abc()" form="ServiceForm">
<option></option>
</select>

并尝试使用 JavaScript:

<script type="text/javascript">
function abc(){
var e = document.getElementById("Region_ID");
var val = e.options[e.selectedIndex].value;
$.post("getSecondDropDown.php",{ Region_ID:val}, function( data ) {
$("#Country_ID").html(data);

});
}
</script>

关于php - 使用 php 和 mysql 的多个下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39722184/

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