我的脚本显示了我之前选择的areaID的所有城市(在选择框中)。
例如- 我选择了区域ID 2我将在第二个选择框中看到所有符合区域2的城市
问题是 - 当我加载页面并希望显示之前选择的城市(来自数据库)
城市选择框中的CITY选项不是来自数据库的cityID上的点。
我需要改变什么?
先谢谢。
<select name='areaID' id='areaID'>
<?PHP
$query = mysql_query("SELECT * FROM `areas` ORDER BY id ASC ");
while($index = mysql_fetch_array($query))
{
$db_area_id = $index['id'];
$db_area_name = $index['name'];
if ($db_area_id == $userDetails['area'])
echo "<option value='$db_area_id' selected>$db_area_name</option>";
else
echo "<option value='$db_area_id'>$db_area_name</option>";
}
?>
</select>
<select id='cityID' name='cityID'> </select>
<script>
<?PHP if ($_POST) { ?>
$(document).ready(function(){
$('#areaID').filter(function(){
var areaID=$('#areaID').val();
var cityID=<?PHP echo $userDetails['cityID'] ?>;
$('#cityID').load('ajax/getCities.php?areaID=' + areaID+'&cityID=' + cityID);
return false;
});
});
<?PHP }else { ?>
$(function () {
function updateCitySelectBox() {
var areaID = $('#areaID').val();
$('#cityID').load('ajax/getCities.php?areaID=' + areaID);
return false;
}
updateCitySelectBox();
$('#areaID').change(updateCitySelectBox);
});
<?PHP } ?>
</script>
getCities.php:
$areaID = (int) $_GET['areaID'];
$second_option = "";
$query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = $areaID ORDER BY id ASC");
while($index = mysql_fetch_array($query2))
{
$id = $index['id'];
$name = $index['name'];
$name = iconv('windows-1255', 'UTF-8', $name);
$second_option .= "<option value='$id'>$name</option>";
}
echo $second_option;
答案 0 :(得分:0)
试
jquery的:
$(document).ready(function(){
$('#areaID').on('change',function(){
var SelectedAreaID = $(this).val();
$.ajax({
type: "POST",
url: "ajax/getCities.php",
data: { areaID: SelectedAreaID }
}).done(function(data) {
$('#cityID').html(data);
});
return false;
});
});
选择框:
<select name='areaID' id='areaID'>
<?PHP
$query = mysql_query("SELECT * FROM `areas` ORDER BY id ASC ");
while($index = mysql_fetch_array($query))
{
$db_area_id = $index['id'];
$db_area_name = $index['name'];
if ($db_area_id == $userDetails['area']){
echo "<option value='$db_area_id' selected>$db_area_name</option>";
}
else{
echo "<option value='$db_area_id'>$db_area_name</option>";
}
}
?>
</select>
<select id='cityID' name='cityID'> </select>
getCities.php
$areaID = $_POST['areaID'];
$second_option = "";
$query2 = mysql_query("SELECT * FROM `cities` WHERE area_id = $areaID ORDER BY id ASC");
while($index = mysql_fetch_array($query2))
{
$id = $index['id'];
$name = $index['name'];
$name = iconv('windows-1255', 'UTF-8', $name);
$second_option .= "<option value='$id'>$name</option>";
}
echo $second_option;