Bootstrap Modal + PHP + AJAX

问题描述 投票:0回答:2

所以我可以证明我是个白痴/新手/。 我正在寻找一种简单的方法让 ajax 将 ID 传输到模态。 然后采用具有 php 的模态并提供要显示的必要变量。

例如。

桌子

===============================

[Button] | Data | Data | Data | 

===============================

[点击按钮(ID)] -> 弹出模态框 -> 姓名:Data,电子邮件:Data,用户名:Data

我不知道这是否有帮助。 我能够弄清楚如何让模态将信息添加到数据库中,但我似乎无法弄清楚如何将数据从 ID 提取到模态并填充它。

感谢您提供的任何帮助!

编辑:(更新) 这是我的索引页面,显示所有手机库存。 “查看”会弹出模式,但会提供随机信息,它是一个活动 ID,但不是订单中当前的 ID。

希望这有帮助。 (我会接受任何帮助或批评)

    <?php
        include "../includes/db_connect.php";
        $page = "chauffeur";
        $pdo = Database::connect();
        if($_SESSION['LoggedIn'] == 1){ }
        elseif($_SESSION['LoggedIn'] == "")
        {
            header("location: http://wcl-wamp/"); 
        }
     ?>
     <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <title>WCL WebApp</title>
        <link href="../css/bootstrap.css" rel="stylesheet">
        <link href="../css/td/style.css" rel="stylesheet">
        <style>
            .body{margin: 0 40px; }
        </style>
    </head>
    <body>
        <?php include('../nav.php'); ?>
    <div class="body">
            <div class="row">
                <h3><b>Phone Inventory</b></h3>
            <div id="modal-results" ></div>
    <?php
          try {
                    $stmt = $pdo->prepare('SELECT * FROM phone_inventory');
                    $stmt->execute();
                    $result = $stmt->fetchAll();
                    if(count($result)) {
                            foreach($result as $row){
                        }
                    }
                } catch (Exception $e) {
                    echo 'ERROR: ' . $e->getMessage();
                } ?>
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h3 class="modal-title" id="myModalLabel"><b>Phone Profile - ID <?= $row['id']; ?></b></h3>
                  </div>
                  <div class="modal-body">
                    <form class="test" role="form">
                      <div class="form-group">
                        <label for="phone_number">Phone #
                        <input type="text" class="form-control" id="phone_number" name="phone_number" value="<?= $row['phone_number']; ?>"></label>
                        <label for="device_id">Device ID
                        <input type="text" class="form-control" id="device_id" name="device_id" value="<?= $row['device_id']; ?>"></label>
                        <label for="device_manufacturer">Device Manufacturer
                        <input type="text" name="device_manufacturer" id="device_manufacturer" class="form-control" value="<?= $row['device_manufacturer']; ?>" /></label>
                        <label for="device_model">Device Model
                        <input type="text" name="device_model" id="device_model" class="form-control" value="<?= $row['device_model']; ?>"/></label>
                        <label for="phone_alias">Phone Alias
                        <input type="text" name="phone_alias" id="phone_alias" class="form-control" value="<?= $row['phone_alias']; ?>"/></label>
                        <label for="chauffeur_number">Chauffeur #
                        <input type="text" name="chauffeur_number" id="chauffeur_number" class="form-control" value="<?= $row['chauffeur_number']; ?>"/></label>
                      </div>
                    </form>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary" id="update">Check Out</button>
                  </div>
                </div>
              </div>
            </div>
<div class="row">
            <table class="table table-striped table-condensed table-hover">
                <thead>
                    <tr>
                        <th></th>
                        <th>Phone #</th>
                        <th>Device ID</th>
                        <th>Device Manufacturer</th>
                        <th>Device Model</th>
                        <th>Phone Alias</th>
                        <th>Chauffeur #</th>
                    </tr>
                </thead>
                <tbody>
                        <?php $sql = 'SELECT * FROM phone_inventory ORDER BY id ASC';
                            foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
echo '<td><a class="btn btn-xs btn-primary" data-toggle="modal" data-id="'. $row['id'] .'" href="#myModal" >View</a></td>';
                                echo '<td>'. $row['phone_number'] .'</td>';
                                echo '<td>'. $row['device_id'] .'</td>';
                                echo '<td>'. $row['device_manufacturer'] .'</td>';                                                                                              
                                echo '<td>'. $row['device_model'] .'</td>';
                                echo '<td>'. $row['phone_alias'] .'</td>';
                                echo '<td>'. $row['chauffeur_number'] .'</td>';
                                echo '</tr>';
                        }
                            Database::disconnect();
                        ?>
                </tbody>
            </table>
        </div>
    </div>  
    <?php include('../includes/js_scripts.php'); ?>
    <script>
        $(document).ready(function() {
            $('.table').dataTable( {
            "sPaginationType": "bootstrap",
            "iDisplayLength": 10
            } );
        } );
    $(".device").click(function(){
        var id = $(this).attr('data-id');
        $("#myModal").find("#id").val(id);
        $("#myModal").dialog("open");
    })


    $(".alert").delay(200).addClass("in").fadeOut(4000);

     $(function() {
//twitter bootstrap script
        $("button#update").click(function(){
                    $.ajax({
                    type: "POST",
                    url: "test.php",
                    data: $('form.test').serialize(),
                    success: function(msg){
                             $("#modal-results").html(msg)
                            $("#myModal").modal('hide');    
                         },
                error: function(){
                    alert("failure");
                    }
                      });
            });
        });
    </script>
</body>
</html>
php ajax twitter-bootstrap modal-dialog
2个回答
0
投票

好吧。我想我明白你需要什么。您有一个结果页面,并且您希望在每个结果中都有一个链接,这将在模式中显示结果的详细信息页面。

所以让我们一步一步来。让详细信息页面的链接正常工作。一旦工作正常,我们就用 AJAX 劫持它,并在模式中显示页面。

Bootstrap 可以轻松执行 AJAX 模式,请查看

remote
选项:

http://getbootstrap.com/javascript/#modals-usage


<a href="detail.php?id=123" data-toggle="modal" href="remote.html" data-target="#modal" data-target="#modal">Link Text</a>

让我知道进展如何,对于新事物并不羞耻:D


0
投票

好吧。我想我明白你需要什么。您有一个结果页面,并且您希望在每个结果中都有一个链接,这将在使用单个模型打开和行编辑数据中的字段的编辑中打开详细表单。

请按照此脚本编辑(更新)单个模型中的数据。

<script type="text/javascript">
    /* if double click on table row then open model and fill all data in form */
    $('#table-id tbody').on('dblclick', 'tr', function() {
        var table = $('#table-id').DataTable();
        var data = table.row(this).data();

        $("#phone_number").val(data.phone_number);
        $("#device_manufacturer").val(data.device_manufacturer);
        $("#device_model").val(data.device_model);
        $("#phone_alias").val(data.phone_alias);
        $("#chauffeur_number").val(data.chauffeur_number);
        $("#device_model").val(data.device_model);

        $("#modal-resultsl").modal('show');
    });

     /* if model close then all field is blank in open model in form */
     $('#con-close-modal').on('hidden.bs.modal',function() {
        /*Clear all input type="text" box*/
        $('#Form-id input[type="text"]').val('');
        /*Clear all input type="number" box*/
        $('#Form-id input[type="number"]').val('');
    });
</script>
© www.soinside.com 2019 - 2024. All rights reserved.