2012-08-08 154 views
1

我想要更改國家下拉列表更改的城市下拉列表的值。那麼現在我堅持選擇在國家的價值點,它說變量$ r是未定義的。 頁:index.php文件使用php-ajax更新另一個下拉列表的更改下拉列表

<form method="post" action="" name="form1"> 
<table width="60%" border="0" cellspacing="0" cellpadding="0"> 
    <tr> 
    <td width="150">Country</td> 
    <td width="150"><select name="country" onChange="getCity('findcity.php?country='+this.value)"> 
    <option value="">Select Country</option> 
    <option value="1">USA</option> 
    <option value="2">Canada</option> 
     </select></td> 
    </tr> 
    <tr style=""> 
    <td>City</td> 
    <td ><div id="citydiv"><select name="city"> 
    <option>Select City</option> 
     </select></div></td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
</table> 

頁:findcity.php

<?php 
$country=$_REQUEST['country']; 
$link = mysql_connect('localhost', 'root', ''); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db('db_ajax'); 
$query="select city from city where countryid=$country"; 
$result=mysql_query($query); 
?> 
<select name="city"> 

<? while($r=mysql_fetch_array($result)) { 

?> 
<option value=""><?php echo $r['city'];?></option> 
<? } ?> 
</select> 

這裏是腳本

<script> 
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; 
    } 



    function getCity(strURL) {  

     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> 
+0

還應該發佈你的jQuery/JavaScript代碼,否則我們不知道你的getCity代碼 – kawashita86 2012-08-08 09:23:10

回答

0

嘗試使用mysql_fetch_assoc代替mysql_fetch_array

+0

沒有它不工作。還是一樣的錯誤 – 2012-08-08 09:33:50

+0

請提供確切的錯誤說明 – Shades88 2012-08-08 09:41:38

0

你想念在<?(write down php here) while($r=mysql_fetch_array($result)) {附近寫「php」。並執行一些調試,比如檢查是否獲取國家/地區的id,而不是檢查查詢是否返回正確的結果,或者在瀏覽器上打印查詢並在數據庫上運行該查詢。

0

改變你的代碼..

<?php 
    while($r=mysql_fetch_array($result)) 
    { 

     echo '<option value="">'.$r['city'].'</option>'; 
    } 
?> 
相關問題