2015-01-04 45 views
0

數據我嘗試以下,但ID沒有工作我怎麼會從數據庫中獲取在下拉菜單

//PHP CODE 

$query = "SELECT kategorite FROM kategorite"; 

$data = mysql_query($conn, $query); 

$makes = array(); 

while($row = mysql_fetch_array($data)) 
{ 
array_push($makes, $row["Lloji"]); 
} 
echo json_encode($makes); 

//JAVASCRIPT CODE 

$(document).ready(function() { 

$.getJSON("getTipin.php", success = function(data) 
{ 
var options = ""; 

for(var i=0; i < data.length; i++) 

{ 

options += "<option value='" + data[i].toLowerCase() + "'>" + data[i] + "</option>"; 

} 

$("#type").append(options); 

$("type").change(); 
}); 
+0

定義「沒有工作」。 – undefined 2015-01-04 12:26:34

+0

是的。我的下拉列表仍然是empy – MrVampo 2015-01-04 12:27:45

+0

您只能從表中選擇'kategorite'('kategorite FROM kategorite'?)字段,所以'$ row'沒有'Lloji'鍵。你還沒有關閉ready函數,缺少'})'。請學習調試。口譯員會告訴你你的代碼有什麼問題。 – undefined 2015-01-04 12:33:31

回答

2

的代碼包含一些小的錯誤,除了那些在其他意見處理。例如,您撥打change(),電話號碼爲$('type'),而不是$('#type')。另外,並非所有的瀏覽器都沒有提供JSON的內容類型。

一般來說這個問題是由兩個部分組成:

從JSON jQuery中

function fillCombo($combo) { 
    $.post('/url/to/php/endpoint', 
     { search: '%' }, 
     function(options) { 
      for (i in options) { 
       $combo[0].options[i] = { 
        value: options[i].value, 
        text : options[i].text 
       }; 
      } 
      $combo.change(); 
     } 
    ); 
} 

fillCombo($('#comboBox'); 

獲取的數據,並輸出JSON

// Here I strongly suggest to use PDO. 

$dbh = new PDO('mysql:host=localhost;port=3306;dbname=database', 'user', 'password', 
    array(
     PDO::ATTR_PERSISTENT  => true, 
     PDO::ATTR_EMULATE_PREPARES => false, 
     PDO::ATTR_ERRMODE   => PDO::ERRMODE_EXCEPTION 
    ) 
); 
$query = "SELECT kat_id, kategorite FROM kategorite WHERE kategorite LIKE :search"; 
$stmt = $dbh->prepare($query); 

// Always check parameter existence and either issue an error or supply a default 
$search = array_key_exists('search', $_POST) ? $_POST['search'] : '%'; 

$stmt->bindParam(':search', $search, PDO::PARAM_STRING); 
$stmt->execute(); 
$reply = array(); 
while ($tuple = $stmt->fetch(PDO::FETCH_NUM)) { 
    $reply[] = array(
     'value' => $tuple['kat_id'], 
     'text' => $tuple['kategorite'], 
    ); 
}; 

// See: http://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type 
Header('Content-Type: application/json'); 

// Adding Content-Length can improve performances in some contexts, but it is 
// needless with any kind of output compression scheme, and if things go as they 
// should, here we have either zlib or gz_handler running. 

// die() ensures no other content is sent after JSON, or jQuery might choke. 
die(json_encode($reply)); 

填充組合框在這種情況下,由於返回的數據與comboBox使用的格式相同,也可以縮短和加速以下內容:

 function(options) { 
      $combo[0].options = options; 
      $combo.change(); 
     } 

一般而言,您希望服務器儘可能少地做些工作(服務器負載成本金錢和影響性能),而且客戶端儘可能少做工作(客戶端負載影響網站的感知和響應度)。什麼樣的數據交換格式是值得思考的東西。

對於很長的沒有分頁的列表,例如,您可能希望通過僅對該選項的文本進行編碼來剪切正在發送的數據。然後,您將發送的

[ 'make1','make2','make3'... ] 

代替

[ { "option": "1", "value": "make1" }, { "option": "2", "value": "make2" }, ... ] 

,並使用較慢的客戶週期來填充組合框。