将选定的数据流传递给模态引导程序 [英] passing selected datarow into modal bootstrap

查看:52
本文介绍了将选定的数据流传递给模态引导程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,每行都有一个按钮,用于显示每条记录的详细信息。我需要的是将选定的数据行传递给模态。

I have a table which has a button in each row to show details of each record. what I need is to pass selected data row to modal.

这是显示数据的PHP代码

here is my PHP code for display data

                <?php 
                include 'koneksi.php';

                $sql = "SELECT UPPER(id_site) AS id_site, UCASE(name_site) AS name_site, witel, UPPER(uplink) AS uplink, port_uplink, ip_uplink, UPPER(olt) AS olt, ip_olt, port_olt, ip_ont FROM data_site ORDER BY witel ASC";
                $result = $conn->query($sql);
                if($result->num_rows > 0){
                    //output data 
                    while ($row = $result->fetch_assoc()) {             
                    echo "<tr>
                            <td>$row[id_site]</td>
                            <td>$row[name_site]</td>
                            <td>$row[witel]</td>
                            <td>$row[olt]</td>
                            <td>$row[ip_olt]</td>
                            <td>$row[port_olt]</td>
                            <td>$row[ip_ont]</td>
                            <td><button type='button' data-id='".$row['id_site']."' class='btn btn-xs btn-success' data-toggle='modal' data-target='#detail'>DETAIL</button></td>
                        </tr>";
                    }
                }
             ?> 

这一个用于模态显示数据

this one for display data in modal

<?php 
                include 'koneksi.php';
                $sql = "SELECT * FROM data_site WHERE id_site = '".$row['id_site']."'";
                $result = $conn->query($sql);
                if($result->num_rows > 0){
                    while ($row = $result->fetch_assoc()) {
                        echo "<table>
                                <tr>
                                <td>ID Site </td>
                                <td>: $row[id_site]</td>
                                </tr>
                                <tr>
                                <td>Nama Site </td>
                                <td>: $row[name_site]</td>
                                </tr>                                   
                                <tr>
                                <td>Witel</td>
                                <td>: $row[witel]</td>
                                </tr>
                                <tr>
                                <td>OLT Hostname  </td>
                                <td>: $row[olt]</td>
                                </tr>
                                <tr>
                                <td>IP OLT </td>
                                <td>: $row[ip_olt]</td>
                                </tr>
                                <tr>
                                <td>Port OLT</td>
                                <td>: $row[port_olt]</td>
                                </tr>
                                <tr>
                                <td>IP ONT</td>
                                <td>: $row[ip_ont]</td>
                                </tr>
                            </table>
                        ";
                    }
                }
              ?>

这是我的模态

<div id="detail" class="modal fade" role="dialog">
          <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
              <div class="modal-header">
                <h4 class="modal-title"></h4>
              </div>
              <div class="modal-body">

              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>

          </div>
        </div>

和我的javascript

and my javascript

<script>
        $('#detail').on('show.bs.modal', function (e) {
            var uniqueId = $(e.relatedTarget).data('id');
            $('.modal-title').html('Detail site ' + uniqueId);
            $('.modal-body').html(uniqueId);
        });
</script> 


推荐答案

您需要的是<$ c内的Ajax调用$ c> $('#detail')。on('show.bs.modal',function(e){} 来获取 $ row ['id_site'的数据]

<script>
    $(document).ready(function() {
        $('#detail').on('show.bs.modal', function (e) {
            var uniqueId = $(e.relatedTarget).data('id');
            //Ajax Method
            $.ajax({
                type : 'post',
                url : 'file.php', //Here you will fetch records 
                data :  'uniqueId='+ uniqueId, //Pass uniqueId
                success : function(response){
                    $('.modal-title').html('Detail site ' + uniqueId);
                    $('.modal-body').html(response);
                }
            });
        });
    });
</script> 

file.php

<?php 
include 'koneksi.php';
if($_POST['uniqueId']) {
    $uniqueId = $_POST['uniqueId']; //escape the string
    $sql = "SELECT * FROM data_site WHERE id_site = '".$uniqueId."'";
    $result = $conn->query($sql);
    if($result->num_rows > 0){
    while ($row = $result->fetch_assoc()) {
        echo "<table>
                <tr>
                <td>ID Site </td>
                <td>: $row[id_site]</td>
                </tr>
                <tr>
                <td>Nama Site </td>
                <td>: $row[name_site]</td>
                </tr>                                   
                <tr>
                <td>Witel</td>
                <td>: $row[witel]</td>
                </tr>
                <tr>
                <td>OLT Hostname  </td>
                <td>: $row[olt]</td>
                </tr>
                <tr>
                <td>IP OLT </td>
                <td>: $row[ip_olt]</td>
                </tr>
                <tr>
                <td>Port OLT</td>
                <td>: $row[port_olt]</td>
                </tr>
                <tr>
                <td>IP ONT</td>
                <td>: $row[ip_ont]</td>
                </tr>
            </table>
        ";
    }
}
}
?>

这篇关于将选定的数据流传递给模态引导程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆