2017-02-25 86 views
2

我想用ajax填充我的選擇選項,當我點擊「詳細」按鈕時。 選項是從數據庫中,我已經試過:如何使用ajax填充選擇選項?

的選擇選項:

<select id="update_listboxstock" size="5" class="form-control"> 

</select> 

按鈕的代碼:

<td style="text-align:center;"> 
     <button onclick="GetMenuDetails('.$row['kode_menu'].')" class="btn btn-warning" data-toggle="modal" data-target="#update_record_modal">Perbarui/Detail</button> 
    </td> 

的Javascript:

function GetMenuDetails(id) { 
     $.get("function_and_ajax/ajax.php",{ 
      ajx:"GetRecipe", 
      kode_menu:id 
     },function(result){ 
      $("update_listboxstock").html(result); 
     }); 
    } 

AJAX:

include("function_connection.php"); 

    if(isset($_GET['ajx'])){ 
     if($_GET['ajx'] == 'GetRecipe'){ 
      $kode_menu = $_GET['kode_menu']; 
      GetRecipe($kode_menu); 
     } 
    } 

功能GetRecipe:

function GetRecipe($kode_menu){ 
      $conn = getConnection(); 
      echo "<option>".$_SESSION["kode_menu"]."</option>"; 
      $query = "SELECT DISTINCT S.NAMA_BARANG AS NAMA_BARANG, MD.JUMLAH AS JUMLAH, S.SATUAN AS SATUAN, S.KODE_STOK AS KODE_STOK FROM STOCKS S, MENUDETAILS MD, MENUS M WHERE S.KODE_STOK = MD.KODE_STOK AND MD.KODE_MENU = '".$_SESSION["kode_menu"]."'"; 
      $conn=getConnection(); 
      $result = $conn->query($query); 
      if ($result->num_rows > 0){ 
       while($row = $result->fetch_assoc()){ 
        echo "<option value=".$row['kode_stok']."> 
         ".$row['NAMA_BARANG']. 
         " - ". 
         $row['JUMLAH']." ".$row['SATUAN']." 
        </option>"; 
       } 
      } 
    } 

對不起,我的英文不好...

回答

1

你有GetMenuDetails js函數的錯誤:

 $("#update_listboxstock").html(result); 

和Not:

 $("update_listboxstock").html(result); 

由於update_listboxstock是此Select的ID。

+0

沒注意到之前,謝謝你的回答! – Sebastian

0
<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
function GetMenuDetails(id) { 
    $.get("function_and_ajax/ajax.php",{ 
     ajx:"GetRecipe", 
     kode_menu:id 
    },function(result){ 
     $.each(result, function(val, text) { 
     $('#update_listboxstock').append($('<option></option>').val(val).html(text)) 
     }); 
     }); 

    }); 
} 



</script> 
</head> 
<body> 

<select id="update_listboxstock" size="5" class="form-control"> 

</select> 
</body> 
</html>