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 コードからコピーしたようですが、正しく更新されていません。$states定義されていないため、php ページはおそらく 500 の req.status を送信しています。- にアップデートしてみてください

$citiesDrop="
    <p><label>Cities</label></p>
    <select name='Cities' id='Cities'>
        " . $Cities . "
    </select>";
于 2013-07-19T19:44:53.070 に答える