2009-04-26 92 views
0

一段時間以來,我一直在努力解決這個問題,但我沒有得出任何結論,所以想在這裏尋求一些幫助。 問題是,我得到一個空白下拉,而不是我應該從數據庫中填充城市列表。數據庫連接是好的,但我沒有得到任何東西在我的下拉列表中。使用php和ajax下拉菜單

這是我在做什麼:

<?php 

require 'includes/connect.php'; - database connection 
$country=$_REQUEST['country']; - get from form (index.php) 
$q = "SELECT city FROM city where countryid=".$country;  
$result = $mysqli->query($q) or die(mysqli_error($mysqli));  
if ($result) { 
?> 
    <select name="city"> 
    <option>Select City</option> 
    $id = 0; 
    <?php while ($row = $result->fetch_object()) { 
     $src = $row->city; 
     $id = $id + 1;  
    ?>  
    <option value= <?php $id ?> > <?php $src ?></option> 
<?php } ?> 
</select> 
<?php } ?> 

AJAX腳本是這樣的:

<script> 
    function getXMLHTTP() { //function to return the xml http object 
      var xmlhttp=false; 
      try{mlhttp=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) {      
         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> 

這是我的表單代碼:

<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">New Zealand</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> 

</table> 
</form> 

回答

1

我認爲問題出在您輸出<option>標籤的地方。

嘗試在您的<select>標記之間使用這段代碼。

<option>Select City</option> 
    <?php 
     $id = 0; 
     while ($row = $result->fetch_object()) { 
     $src = $row->city; 
     $id = $id + 1;  
    ?>   
    <option value="<?php echo htmlspecialchars($id,ENT_QUOTES) ?>"><?php echo htmlspecialchars($src) ?></option> 
<?php } ?> 

編輯:爲了澄清,你沒有足夠的$id$src變量之前任何echo語句。我添加了htmlspecialchars()作爲生成正確轉義的html的習慣。

0

有幾件事情嘗試:

  1. 如果您在您的瀏覽器中手動請求findcity.php並知道您在數據庫中存在的城市,我會返回正確的HTML嗎?

  2. 嘗試使用FireBug或其他JavaScript調試器,在onreadystatechange函數中設置斷點並查看返回的值是否與預期的一樣。在函數的第一行設置斷點。

0
<input name="acname" type="text" id="acname" value="" maxlength="9"> 
Account Name </p> 

<select name="src"> 
    <option value="number"> :::: Select ::::</option> 
    <option value="did">DID</option> 
    <option value="tfn">TFN</option> 
</select> 
<span id="errmsg"></span> DID/TFN </p> 

<select name="did" onchange='OnChange(this.form1.did);' > 
    <option value=""> :::: Select ::::</option> 
    <? $qry1 = mysql_query("SELECT * FROM ".NUMBERS." where Flag='1'") or die(mysql_error()) ; 
    while($res1 = mysql_fetch_array($qry1)) { ?> 
    <option value="<?=$res1['Numbers'] ?>"><?=$res1["Numbers"]?></option> 
     <? } ?> 
</select> 

SOURCE