0

動的に変更できるドロップダウン メニューを作成するための優れたチュートリアルを見つけました。

ここにリンクがあります。

http://roshanbh.com.np/2008/01/populate-triple-drop-down-list-change-options-value-from-database-using-ajax-and-php.html

ここにデモがあります

http://roshanbh.com.np/dropdown/


コードを少しいじっていて、状態を選択する部分をスキップしています。

これがfindCity.phpファイルです

<!--//---------------------------------+
//  Developed by Roshan Bhattarai    |
//  http://roshanbh.com.np           |
//  Contact for custom scripts       |
//  or implementation help.          |
//  email-nepaliboy007@yahoo.com     |
//---------------------------------+-->
<?
#### Roshan's Ajax dropdown code with php
#### Copyright reserved to Roshan Bhattarai - nepaliboy007@yahoo.com
#### if you have any problem contact me at http://roshanbh.com.np
#### fell free to visit my blog http://php-ajax-guru.blogspot.com
?>

<? $country = $_GET['country'];
$link  = mysql_connect("snip","snip","snip");
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('snip');
$query="SELECT city FROM location WHERE country='$country'";
$result=mysql_query($query);

?>
<select name="city">
<option>Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>

国変数でfindCity.phpを開くと正常に動作します

http://globatum.com/admin/findCity.php?country=United%20States

国リストの値がこの関数を適切に通過しない理由がわかりません

function getCity(country) {
        var strURL="findCity.php?country="+ country;
        var req = getXMLHTTP();

        if (req) {

            req.onreadycountrychange = 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);
        }

    }

ここに何かアイデアはありますか?

追記事項

function getXMLHTTP() { //function to return the xml http object
        var xmlhttp=false;  
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {       
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }

        return xmlhttp;
    }
4

2 に答える 2

2

ページに JavaScript エラーがありましたが、@nevermind が指摘してくれました。それは問題ありませんが、別の問題があり、それはあなたの誤解のようです. この線

req.onreadycountrychange = function() {

意味がありません。代わりに次のようにする必要があります。

req.onreadystatechange = function() {

関数の名前は、onreadystatechangeこのコンテキストでは少し混乱しますが、「州/国」のように「州」とは何の関係もありません。これは、XMLHttpRequestオブジェクトの現在の状態を参照しています。基本的に、その関数onreadystatechangeは、リクエスト オブジェクトの状態が更新されるたびに呼び出されます。

詳細については、こちらをご覧ください: http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

于 2013-07-05T21:00:09.580 に答える
0

テストページのコードに構文エラーがあります:

if (req.readyState == 4 {

もちろん、次のようにする必要があります。 if (req.readyState == 4 )

これで問題が解決しない場合は、別の方法を試すことができます。:)

于 2013-07-05T20:50:14.127 に答える