将 PHP 变量传递给引导模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34693863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:09:55  来源:igfitidea点击:

Pass PHP variable to bootstrap modal

phpjquerymysqltwitter-bootstrap

提问by Rajbir

I have a button that is created using a while loop. so the while loop creates a table row for each row in a mysql table.

我有一个使用 while 循环创建的按钮。所以while循环为mysql表中的每一行创建一个表行。

I have one line of code for the button which is created again for each row of the table and each button has a different value based on the idfor that record.

我有一行代码用于按钮,该代码为表的每一行再次创建,并且每个按钮具有基于id该记录的不同值。

$order .= '<td><a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="'.$row['ID'].'">Edit</a></td>';

The problem is that I want to use that $row['ID']and view it in a modal so I can retrieve the record with the same ID using mysqli query.

问题是我想使用它$row['ID']并以模式查看它,以便我可以使用 mysqli 查询检索具有相同 ID 的记录。

Here is the code for the modal.

这是模态的代码。

<div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Edit Data</h4>
                </div>
                <div class="modal-body">
                    i want to save the id in a variable here so i can use it in a php script for this modal
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

回答by Mark Gerryl Mirandilla

put this inside your loop

把它放在你的循环中

<button type="button" class="btn btn-success" data-toggle="modal" data-target="#message<?php echo $row['id'];?>">Message</button>
<div id="message<?php echo $row['id'];?>" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p>Some text in the modal.</p>
        <?php echo $row['id'];?>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

回答by Shehary

If, I got you correct.

如果,我猜对了。

The modal trigger button with $row['ID']

模态触发按钮 $row['ID']

$order .= '<td><a href="#myModal" class="btn btn-default btn-small" id="custId" data-toggle="modal" data-id="'.$row['ID'].'">Edit</a></td>';

Bootstrap Modal event to fetch the $row['ID']value with Ajax method

$row['ID']使用 Ajax 方法获取值的Bootstrap Modal 事件

$(document).ready(function(){
    $('#myModal').on('show.bs.modal', function (e) {
        var rowid = $(e.relatedTarget).data('id');
        $.ajax({
            type : 'post',
            url : 'fetch_record.php', //Here you will fetch records 
            data :  'rowid='+ rowid, //Pass $id
            success : function(data){
            $('.fetched-data').html(data);//Show fetched data from database
            }
        });
     });
});

Modal HTML

模态 HTML

<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Edit Data</h4>
            </div>
            <div class="modal-body">
                <div class="fetched-data"></div> //Here Will show the Data
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

Last Create fetch_record.php, retrieve the record and show in modal

最后创建fetch_record.php,检索记录并以模式显示

<?php
//Include database connection
if($_POST['rowid']) {
    $id = $_POST['rowid']; //escape string
    // Run the Query
    // Fetch Records
    // Echo the data you want to show in modal
 }
?>

回答by jerryurenaa

adding the modal in a loop is auto duplicating the html content when it is not really necessary! it is not a good practice to add a modal to each item in a loop instead call the data only when it is requested!

在循环中添加模态是在不需要时自动复制 html 内容!将模式添加到循环中的每个项目而不是仅在请求时调用数据不是一个好习惯!

here is an example add the following button to a loop in php

这是一个示例,将以下按钮添加到 php 中的循环中

<button data-id="<?php echo $note['id']; ?>"  onclick="$('#dataid').text($(this).data('id')); $('#showmodal').modal('show');">Click me </button>

on your modal you can know use that id to make a request to your db or any json

在您的模态上,您可以知道使用该 ID 向您的数据库或任何 json 发出请求

example assuming you are using a modal called showmodal use this info to make a new request to your database or desire class or function!

假设您正在使用名为 showmodal 的模式的示例,请使用此信息向您的数据库或所需的类或函数发出新请求!

<?php $dataid = '<span id="dataid"/>'; echo $dataid; ?>

The echo $dataid is only to show you that the id of the clicked item in the loop really works.

echo $dataid 只是为了向您显示循环中单击项的 id 确实有效。

as you can see here we are only adding 1 single html template in the loop and we are also only calling data when it is being requested! this will save memory and also will be faster.

正如您在这里看到的,我们只在循环中添加了 1 个单独的 html 模板,而且我们也只在请求数据时调用数据!这将节省内存,也将更快。

I hope this helps many people

我希望这能帮助很多人