所以我想从服务器上获取网站更新状态和城市列表。
我的状态代码似乎运行正常,我遇到的问题是更新城市列表。我之前从这里调整了答案,虽然这很可能是我的错,但似乎没有用。
这是状态php代码。
<?php
$con= mysqli_connect($host, $user, $pass, $database);
if($debug){
echo $host,$user,$pass,$database;
}
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$states = '';
$resultState = mysqli_query($con,"SELECT DISTINCT State FROM CitiesStates");
while($row = mysqli_fetch_array($resultState))
{
if($debug){
echo $row['State'];
}
$states .="<option>" . $row['State'] . "</option>";
}
$statesDrop="
<p><label>States</label></p>
<select name='States' id='States' onchange='getCity(this.value))'>
" . $states . "
</select>";
echo $statesDrop;
mysqli_close($con);
?>
所以选择它应该调用这个函数。
<script type="text/javascript">
function getCity(stateId)
{
var strURL="findCity.php?state="+stateId;
var req = getXMLHTTP();
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4) // only if "OK"
{
if (req.status == 200)
{
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
</script>
调用此php文件。
$stateId=intval($_GET['state']);
$con= mysqli_connect($host, $user, $pass, $database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$Cities = '';
$resultCity = mysqli_query($con,"SELECT City FROM CitiesStates WHERE State='$stateId'");
while($row = mysqli_fetch_array($resultCity))
{
if($debug){
echo $row['City'];
}
$Cities .="<option>" . $row['City'] . "</option>";
}
$citiesDrop="
<p><label>Cities</label></p>
<select name='Cities' id='Cities' onchange=''>
" . $Cities . "
</select>";
echo $citiesDrop;
mysqli_close($con);
?>
另外我的getXMLhttp()函数,因为这似乎是问题
function getXMLHTTP() {
var x = false;
try {
x = new XMLHttpRequest();
}catch(e) {
try {
x = new ActiveXObject("Microsoft.XMLHTTP");
}catch(ex) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1) {
x = false;
}
}
}
return x;
}
答案 0 :(得分:1)
我认为这是由findCity.php
$citiesDrop="
<p><label>States</label></p>
<select name='States' id='States' onchange='getCity(this.value))'>
" . $states . "
</select>";
看起来你是从你的状态php代码复制它,但没有正确更新它。您的php页面可能正在发送500的req.status,因为$states
未定义。尝试更新到 -
$citiesDrop="
<p><label>Cities</label></p>
<select name='Cities' id='Cities'>
" . $Cities . "
</select>";