我已经尝试过这个例子,但不确定我哪里出错了——我已经创建了数据库和相关字段。当我选择英国时,它给了我一个空白的结果。这是我的脚本
主文件
<?php
$connect=mysql_connect("localhost","root","") or die("Cannot connect");
$dbselect=mysql_select_db("test");
$sql_country = "SELECT * FROM COUNTRY";
$result_country = mysql_query($sql_country) or die("Query problem");
echo "<form method='POST'>";
echo "<select name='country' onChange='get_cities(this.value)'>"; //get_cities is defined below
echo "<option value=''></option>";
while($row_country = mysql_fetch_array($result_country))
{
echo "<option value='".$row_country['id']."'>".$row_country['country']."</option>";
}
echo "</select>";
echo "<select name='city' id='city'></select>"; //We have given id to this dropdown
echo "</form>";
?>
<script type="text/javascript">
function get_cities(country_id)
{
$.ajax({
type: "POST",
url: "cities.php", /* The country id will be sent to this file */
beforeSend: function () {
$("#city").html("<option>Loading ...</option>");
},
data: "country_id="+country_id,
success: function(msg){
$("#city").html(msg);
}
});
}
</script>
城市.php
<?php
// Code for cities.php
$connect=mysql_connect("localhost","root","") or die("Cannot connect");
$dbselect=mysql_select_db("test");
$country_id = $_REQUEST['country_id'];
$sql_city = "SELECT * FROM CITY WHERE country_id = '".$country_id."'";
$result_city = mysql_query($sql_city);
echo "<select name='city'>";
while($row_city = mysql_fetch_array($result_city))
{
echo "<option value='".$row_city['id']."'>".$row_city['city']."</option>";
}
echo "</select>";
?>