gpt4 book ai didi

php - 国家和城市下拉列表

转载 作者:行者123 更新时间:2023-11-30 21:47:15 38 4
gpt4 key购买 nike

我是 Ajax 和 PHP 的新手,遇到动态下拉状态和城市的问题。虽然我已经在 stackOverflow 中检查了很多答案,但我无法清楚地了解我们应该如何成功编码以获得所需的结果。

问题:无法获得城市、国家和州的下拉值,但已成功填充。

country_back.php [使用 country_id 为各州动态生成下拉列表]

<?php 

$con_id=$_POST['id'];


$con=mysqli_connect("localhost","root","","countries");
$data=mysqli_query($con,"select * from states where country_id='$con_id' ");
echo "<select id='st'>";
while($row=mysqli_fetch_array($data))
{
echo "<option value=".$row['id'].">".$row['name']."</option>";
}
echo "</select>";



?>

ajax文件

$("#st").change(function(){
var s=$(this).val();
alert(s); //no value being shown with alert.
$.ajax=({
url:"state_back.php",
method:"post",
data:{stid:s},
dataType:"html",
success:function(strMsg){
$("#city").html(strMsg);

}

})

})

HTML 表单

<form method="post">

<div id="city">
<select>
<option>Cities</option>
</select>
</div>
</form>

state_back.php 使用 state_id 为城市动态生成下拉列表

<?php

$stid=$_POST['stid'];

$con=mysqli_connect("localhost","root","","countries");
$data=mysqli_query($con,"select * from cities where state_id='$stid' ");
echo "<select>";
while($row=mysqli_fetch_array($data))
{
echo "<option>".$row['name']."</option>";
}
echo "</select>";


?>

最佳答案

更改您的 ajax 代码:

   $(document).on('change', '#st', function(e){
var s=$('#st').val();
alert(s); //no value being shown with alert.
$.ajax({
url:"state_back.php",
method:"post",
data:{stid:s},
dataType:"html",
success:function(strMsg){
alert(strMsg);
$("#city").html(strMsg);
}
});
});

关于php - 国家和城市下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48879707/

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