2011-12-09 39 views
0

我有一個DDL(#engine),需要通過具有基於選定了#make DDL一個MySQL表中設置其選項值設置選擇選項。jQuery的Ajax調用從一個MySQL表

我有以下腳本傳遞到PHP文件 - 我知道PHP文件是罰款,我可以輸出查詢的結果independantly。

但是,我不能得到選項值更新基於返回數組上...也許使用GET的結果?這是我第一次嘗試通過AJAX(通常只是用它來更新數據表等)

有什麼不對勁,我腳本返回?

$(document).ready(function() { 

$("select#make").change(function(){ 
    $('#engine').find('option').remove().end(); //clear the engine ddl 
    var make = $(this).find("option:selected").val(); //Need the value not the text 

    $.ajax({ 
     url:'get-engine.php', 
     type:'GET', 
     data:{engine:make}, 
     dataType:'json', 
     cache:false, 
     success:function(data){ 


     var ddl = document.getElementById('engine');      

     for(var c=0;c<obj.length;c++) 
       {    
       var option = document.createElement('option'); 
       option.value = obj[c]; 
       option.text = obj[c];       
       ddl.appendChild(option); 
       } 


    }, 
     error:function(jxhr){ 
     alert(jxhr.responseText); 

    } 
    }); 

}); 
}); 

的GET-engine.php ..

$make = $_GET['engine']; 

    $query= ("SELECT * FROM tb_engine where make_id='$make'"); 
    $result = mysql_query($query); 
    $temp = array(); 

    while ($row = mysql_fetch_assoc($result)) { 

    if(empty($temp)) 
    { 
     $temp=array($row['engine_type']); 
    } 
    else 
    { 
     array_push($temp,$row['engine_type']); 
    } 

    } 
    echo (json_encode($temp)); 
    ?> 

它至今拒絕更新,不能完全找到,爲什麼?提前對所有建議

+0

你明白了'json'響應回來?你有沒有檢查過螢火蟲控制檯... – Rafay

+0

嗨,是的,確實如此 - 我可以看到firebug中的響應數組...發回數組,如預期,它似乎只是我的腳本不創建基於返回數組的選項值? – Marc

+0

你可以發表你的JSON響應 – Rafay

回答

1

謝謝你可以試試

//json is the json you have recieved in the success handler 

$("#engine").empty(); 
$.each(json, function(index, item) { 
      $("<option/>",{value:item,text:item}).appendTo("#engine"); 

     }); 

DEMO