Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't show the modal in codeigniter using ajax

I'm working with codeigniter, and i want to show the detail data in modal, the detail data is from another table.

I have a button like this:

<button class="btn btn-info" onclick="detail_barang(&quot;<?php echo $tbrang->id_barang;?>&quot;)">Detail</button>

And this is my detail_barang function:

function detail_barang(id)
{
  $('#form')[0].reset(); // reset form on modals

  $.ajax({
    url : "<?php echo site_url('admin/Barang/barang/ajax_detail_barang/')?>/" + id,
    type: "GET",
    dataType: "JSON",
    success: function(data)
    {
        $('#modal_detail').modal('show'); // show bootstrap modal when complete loaded
        $('.modal-title').text('Detail Barang'); // Set title to Bootstrap modal title

    },
    error: function (jqXHR, textStatus, errorThrown)
    {
        alert('Kesalahan!');
    }



    });
}

Modal Script:

<div class="modal fade" id="modal_detail">
<div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h3 class="modal-title">Detail Barang</h3>
        </div>
        <div class="modal-body">
          <table class="table table-striped" id="tblGrid">
            <thead id="tblHead">
              <tr>
                <th>ID Barang</th>
                <th>No Inv</th>
                <th class="text-right">Kondisi</th>
              </tr>
            </thead>
            <tbody>
                <?php
                foreach($detail_barang as $detil)
                {?>
                    <tr>
                        <td><?php echo $detil->id_barang ?></td>
                        <td><?php echo $detil->no_inv ?></td>
                        <td><?php echo $detil->kondisi ?></td>
                    </tr>
                <?php } ?>
            </tbody>
          </table>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default " data-dismiss="modal">Close</button>
        </div>

      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div><!-- /.modal -->

ajax_detail_barang function:

public function ajax_detail_barang($id)
{
    $where = array
    (
        'id_barang' => $id,
        'kondisi' => 'Ada'
    );

    $data['detail_barang'] = $this->modelku->get_by_id('detail_barang', $where)->result();
    echo json_encode($data);
}

get_by_id function:

public function get_by_id($table, $where)
{
    $this->db->from($table);
    $this->db->where($where);
    $query = $this->db->get();

    return $query->row();
}

But when i click the button, the error appears "Kesalahan!" whats wrong my code?

like image 452
masdap Avatar asked Dec 19 '25 04:12

masdap


1 Answers

The problem i see is in how u are using the returned data from ur model

public function get_by_id($table, $where)
{
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();

return $query->row();
//at this point it already contains the data u need
}

but here u are using result();

$data['detail_barang'] = $this->modelku->get_by_id('detail_barang', $where)->result();
//i tried and u cant use result method if u already executed row method.

In short, remove result()

Hope my answer helps u.

like image 111
Francisco Hahn Avatar answered Dec 21 '25 16:12

Francisco Hahn