简体   繁体   English

无法使用ajax在codeigniter中显示模态

[英]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. 我正在使用codeigniter,我想以模态显示详细数据,详细数据来自另一个表。

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: 这是我的detail_barang函数:

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: ajax_detail_barang函数:

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: get_by_id函数:

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!" 但是当我点击按钮时,错误出现“Kesalahan!” whats wrong my code? 什么错了我的代码?

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(); 但是你在这里使用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. 希望我的回答能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果使用Codeigniter成功,则使用Ajax将数据插入数据库并显示模式 - insert data to database using ajax and show modal if success with codeigniter 我可以使用ajax从数据库中加载数据,并以模态包括ckeditor的形式显示,但是我无法编辑/保存这些数据。 我用了Codeigniter 3 - I can load data from database with ajax and show in modal include ckeditor but i can't edit/save those data. I used Codeigniter 3 在带有JQuery ajax的codeigniter中使用模式添加图像 - using modal add image in codeigniter with JQuery ajax 在codeigniter中使用AJAX显示数据的模态 - Modal to display data using AJAX in codeigniter 使用AJAX在codeigniter中更新表单模式引导程序 - UPDATE form modal bootstrap in codeigniter using AJAX 使用Codeigniter以模态显示带有jquery的图像 - Show image with jquery in modal using Codeigniter 如何在php codeigniter中使用ajax从数据库中获取数据并以模式显示? - How can I get data from database using ajax in php codeigniter and display it in modal? 如何使用 ajax-jquery 使用 PHP-CodeIgniter 将选定的复选框数据设置为模态 - How can use ajax-jquery to get selected checkbox data to modal using PHP-CodeIgniter 如何使用ajax在模态窗口中显示pdf? - How to show pdf in a modal window using ajax? 使用AJAX和JavaScript在点击时显示模式 - Show modal on click using AJAX and JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM