简体   繁体   English

以查询方式显示数据

[英]Display data in modal from query

I have this table with data and a link to display detail into modal 我有这张表,上面有数据,还有一个链接,可将详细信息显示为模式

<td><a data-toggle="modal" class="btn" href="#modal_detail" id="<?php echo $rows['id']; ?>">Detail</a></td>

and this is the modal 这是模态

<div id="modal_detail" class="modal hide fade">
    <div class="modal-header">
      <button class="close" data-dismiss="modal">&times;</button>
      <h3>Detail Product</h3>
    </div>
    <div class="modal-body">            
        <div id="modalContent" style="display:none;">
        Here i want to display data from query about the detail of a product (name, price etc)      
        </div>
    </div>
    <div class="modal-footer">
      <a href="#" class="btn btn-info" data-dismiss="modal" >Close</a>
    </div>
</div>

and this is the php file 这是PHP文件

if(isset($_GET['detail'])) {
    include_once('../libs/class.database.php');
    $k = new Database();
    $link=$k->connect(); 
    $query = $link->query("SELECT name,price FROM products ORDER BY date_created ASC");
    $result=$query->fetchAll();
}

for the javascript is like below 对于JavaScript如下

$("a[data-toggle=modal]").click(function() {
    var id_beli = $(this).attr('id');

    $.ajax({
        cache: false,
        type: 'GET',
        url: 'controller/modalPembelian.php',
        data: 'detail=' + id_beli,
        success: function(data) {
            $('#modal_detail').show();
            $('#modalContent').show().html(data); //this part to pass the var
        }
    });
});

My Question is how to display the data from query in the php file (product detail) int o the modal window. 我的问题是如何在模态窗口中显示来自php文件(产品详细信息)中查询的数据。 The modal just fine, it can show and hide, but the data arent there. 模态就好了,它可以显示和隐藏,但是那里没有数据。

I have tried many ways and search for tutorials but it still the same. 我尝试了很多方法并搜索了教程,但是仍然一样。

Can anyone show me some example how to display the data using ajax into twitter bootstrap modal. 谁能给我示范一些如何使用ajax将数据显示到twitter bootstrap modal中的示例。

You can also use find() and place the data to html() 您也可以使用find()并将data放置到html()

success: function (data) {
   console.log(data);
   modal.find('#modalContent').html(data);
}

Full description here: Using Bootstrap Modal with AJAX to import data from MySQL 此处有完整描述:结合使用Bootstrap Modal和AJAX从MySQL导入数据

check out jschr's bootstrap modal 查看jschr的引导模态

its a bootstrap modal plugin that allows you to insert data into the modal using ajax calls, and also displays a loading image while we wait for the data to be retrieved. 它是一个引导模态插件,该插件可让您使用ajax调用将数据插入模态中,并在我们等待数据检索时显示加载图像。

another way that I'm just thinking about would be to use the events to try and load the data, this is an example and not 100% tested, but I believe it should work 我正在考虑的另一种方法是使用事件来尝试加载数据,这只是一个示例,未经100%的测试,但我认为它应该可以工作

$('#modal_detail').on('show', function(){
  var id_beli = $(this).attr('id');
  $('#modalContent').html('Loading..')

  $.ajax({
      cache: false,
      type: 'GET',
      url: 'controller/modalPembelian.php',
      data: 'detail=' + id_beli,
      success: function(data) {
        $('#modalContent').html(data); //this part to pass the var
      }
  });
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM