2

I want to populate my select option using ajax, when i clicked the "Detail" button. the option is from the database, i've tried :

The Select Option :

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

</select>

Button's Code :

<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);
        }
    }

function 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>";
                }
            }
     }

Sorry for my bad English...

0

2 Answers 2

1

You have an error in GetMenuDetails js function :

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

and Not :

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

Since update_listboxstock is the ID of this Select.

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.