我有一个国家城市数据库,显示在选择框中,一切正常。What i want when state select box selected to display city results in a div or another element. 当为城市结果定义选择框时没有问题,但如果我使用 div 会给出错误消息
注意:未定义的索引:第 3 行 C:\wamp\www\list\post.php 中的国家/地区。
这是我的代码,谢谢。
post.php
include("connect.php");
$country = $_POST['country'];
$query = mysqli_query($connect, "select * from state where country_id='$country'");
while( $list = mysqli_fetch_array( $query ) ) {
echo '<option value=' . $list["id"]. '>' . $list["state_name"] . '</option>';
}
$state = $_POST['state'];
$query = mysqli_query($connect, "select * from city where state_id='$state'");
while( $list = mysqli_fetch_array( $query ) ) {
echo $list["city_name"];
}
jQuery代码
$( "#countries" ).change( function(){
$("#states").empty();
var val = $(this).val();
$.post("post.php", {country:val}, function(a) {
$("#states").append(a);
});
});
$( "#states" ).change( function(){
$("#cities").empty();
var val = $(this).val();
$.post("post.php", {state:val}, function(a) {
$("#cities").append(a);
});
});
索引.php
<?php include("connect.php"); ?>
<select id="countries">
<option>select</option>
<?php
$query = mysqli_query($connect, 'select * from country');
while( $list = mysqli_fetch_array( $query ) ) {
echo '<option value=' . $list["id"]. '>' . $list["country_name"] . '</option>';
}
?>
</select>
<select id="states">
<option>select</option>
</select>
<div id="cities"></div>