将选定的数据流传递给模态引导程序

时间:2016-03-09 20:57:39

标签: javascript php modal-dialog bootstrap-modal

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

这是我显示数据的PHP代码

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

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

1 个答案:

答案 0 :(得分:1)

您需要的是$('#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> 

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