1

所以我试图让网站从服务器更新州和城市列表。

我的州代码似乎工作正常,我遇到的问题是更新城市列表。我之前从这里改编了一个答案,它似乎不起作用,尽管这很可能是我的错。

这是状态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;
    }
4

1 回答 1

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>";
于 2013-07-19T19:44:53.070 回答