将选定的数据行传递到模态引导程序

passing selected datarow into modal bootstrap(将选定的数据行传递到模态引导程序)
本文介绍了将选定的数据行传递到模态引导程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>";
                    }
                }
             ?> 

这个用于模态显示数据

<?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> 

推荐答案

你需要的是 $('#detail').on('show.bs.modal', function (e) 内部的 Ajax 调用{ } 根据 $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> 

文件.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>
        ";
    }
}
}
?>

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)