2013-01-11 35 views
0

在我的網頁我有以下代碼阿賈克斯依賴下拉值不取的價值觀

hostadd.php

<script language="javascript" type="text/javascript"> 
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 getState(countryId) {  
    var strURL="findState.php?country="+countryId; 
    var req = getXMLHTTP(); 
    if (req) { 

     req.onreadystatechange = function() { 
      if (req.readyState == 4) { 
       if (req.status == 200) { 
        document.getElementById('statediv').innerHTML=req.responseText;      
       } else { 
        alert("There was a problem while using XMLHTTP:\n" + req.statusText); 
       } 
      }    
     }   
     req.open("GET", strURL, true); 
     req.send(null); 
}  
} 

</script> 


<div style="margin-left: 51px;">State: 
    <select name="country" id="country" onChange="getState(this.value)" style="margin-left: -3px;"> 
     <option ></option> 

     <?php $qr = mysql_query("select * from ctm_state "); 

     while($data= mysql_fetch_array($qr)) 

     {?> 
      <option value="<?php echo $data['id']; ?>"><?php echo $data['state']; ?></option> 
     <?php } ?> 
    </select> 
</div> 

findstate.php

<? $country=intval($_GET['country']); 
include("../config/config.php"); 
$query="SELECT id,location FROM ctm_locationname WHERE stateid='$country'"; 
echo $query; 
$result=mysql_query($query); 
if($result) 
{ 
    echo "success"; 
} 
else 
{ 
    echo "cant fetch"; 
} 
?> 
<div style=""> location:<select name="state" id="state"> 
<option></option> 
<? while($row=mysql_fetch_array($result)) { ?> 
<option value=<?=$row['location']?>><?=$row['location']?></option> 
<? } ?> 
</select></div> 

但顯示「使用xml http請求時出現問題」的錯誤。我無法找到我犯了什麼錯誤。任何人都可以幫我解決這個問題

+1

使用jquery + ajax有什麼困難? –

+1

將這個指向適當的xml對象。 http://www.w3schools.com/ajax/ajax_xmlfile.asp –

+1

這是一個簡單的ajax例子,可以幫助你理解事物。 http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first –

回答

1

我按你的var strURL="findState.php?country="+countryId;是不正確的。

檢查您的瀏覽器錯誤控制檯,您可以在那裏看到錯誤。

如果它顯示404頁面未找到,這是因爲您的strURL不正確檢查拼寫或路徑。