当表单的选择值发生变化时,我正在尝试使用 ajax 函数。它应该显示基于所选州的城市列表。但是,它什么也没做。
城市.php
<script type="text/javascript">
function change_state(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("cityselector").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","changestate.php?changed_state="+str,true);
xmlhttp.send();
}
</script>
<div class="city-switcher">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get" id="cityselector" style="position: absolute; top: 15px; left: 310px;">
<select name="city" onchange="document.forms['cityselector'].submit()" style="font-size: 1.5em;">
<?php
$selected_state = select_single("STATE", "locations", "NAME='$location'", "");
$cities = select("*", "locations", "STATE='$selected_state'", "");
foreach ($cities as $city){
echo '<option value="'.$city['NAME'].'" '.($location == $city['NAME'] ? 'selected="selected"' : '').'>'.$city['NAME'].'</option>';
}
?>
</select>
</form>
<select name="state" onchange="change_state(this.value)" style="font-size: 1.5em;">
<?php
$states = select("DISTINCT STATE", "locations", "", "ORDER BY STATE ASC");
$selected_state = select_single("STATE", "locations", "NAME='$location'", "");
foreach ($states as $state){
echo '<option value="'.$state['STATE'].'" '.($selected_state == $state['STATE'] ? 'selected="selected"' : '').'>'.$state['STATE'].'</option>';
}
?>
</select>
</div>
更改状态.php
<?php
include '../config.php';
include '../library.php';
$changed_state = $_GET['changed_state'];
echo '<select name="city" onchange="document.forms[\'cityselector\'].submit()" style="font-size: 1.5em;">';
$cities = select("*", "locations", "STATE='$changed_state'", "");
foreach ($cities as $city){
echo '<option value="'.$city['NAME'].'" '.($location == $city['NAME'] ? 'selected="selected"' : '').'>'.$city['NAME'].'</option>';
}
echo '</select>';
?>
谁能告诉我我做错了什么。
谢谢,