0

How to show data inside select option from MySQL?

I try to created it, but doesn't work

My Controller

public function getItemCategory(Request $request)
{
    $itemcategory = DB::table('tbl_item_category')->pluck('id_item_category','itemcategory_name');
    return response()->json($itemcategory);
}  

My Jquery

  var i = 0;
  $("#add-more-item").click(function () {
    ++i;
    $.ajax({
        type:"GET",
        url:"/admin-master/get_item_category",
        dataType: 'JSON',
        success:function(res){
          $(".add-more").empty();
            $(".add-more").append(
              '<tr>',
                '<td><input type="text" name="item_code['+i+']" class="form-control" placeholder="Kode Barang" required>',
                '<td><input type="text" name="item_name['+i+']" class="form-control" placeholder="Nama Barang" required</td>',
                '<td><select class="form-control" name="item_category['+i+']">',
                $.each(res,function(itemcategory_name,id_item_category){
                  $(".add-more").append(
                    '<option value="'+id_item_category+'">'+itemcategory_name+'</option></select></td>'
                  );
                }),
              '</tr>'
            );
        }
    });
    
  });

And the result doesn't become a select option, the result is messy but I managed to get the data

result

mfahmifadh
  • 87
  • 1
  • 10
  • Please don't post images of code, but post the actual code itself – Carsten Løvbo Andersen Oct 19 '21 at 06:10
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 19 '21 at 06:29
  • Sorry, i've edit my question – mfahmifadh Oct 19 '21 at 06:31
  • you call .append( ) inside another $(".add-more").append() this is not good – user1150331 Oct 19 '21 at 07:14
  • create your html before then append it is better – user1150331 Oct 19 '21 at 07:15
  • I've tried not to call $(.add-more").append(, not in .append. But the select option instead appears according to the amount of data in the database. How can I display the options according to the data in the database? – mfahmifadh Oct 19 '21 at 07:33
  • There are so many examples here on SO, why not try some of those working answers? https://stackoverflow.com/q/10870667/6089612 https://stackoverflow.com/q/42451698/6089612 https://stackoverflow.com/q/22991067/6089612 https://stackoverflow.com/q/17367332/6089612 https://stackoverflow.com/q/18979369/6089612 https://stackoverflow.com/q/29204934/6089612 – Don't Panic Oct 19 '21 at 08:09
  • sorry if my question is confusing, I have tried it, if only display data from mysql with select option in jquery I can, but how to display select option with html table? because I use this to add more. – mfahmifadh Oct 19 '21 at 08:22
  • do you have any reference to use html table and foreach with select option in ajax – mfahmifadh Oct 19 '21 at 08:49
  • ok now after double check you add this closing tag after each option try to add them after adding option – user1150331 Oct 20 '21 at 06:39

0 Answers0