【问题标题】:Call a PHP function on a Bootstrap Modal using AJAX使用 AJAX 在 Bootstrap Modal 上调用 PHP 函数
【发布时间】:2015-04-23 18:02:24
【问题描述】:

我有一个使用 PHP 循环在我的页面上回显的用户列表。当我单击每个用户的名称时,会弹出一个 Bootsrap 模式并显示用户的更多详细信息。链接如下所示。

<a href="#user"  data-toggle="modal"  data-id="{$user['id']}">UserName</a>

如您所见,我将用户 ID 传递给 Bootstrap 模式,以便我可以通过在 Bootstrap 模式中调用 get_user_by_id() 来使用它来检索用户的其他信息。

模态看起来像这样

<div class="modal fade" id="user">
    <div class="modal-header">
        <button class="close" data-dismiss="modal">×</button>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
        <?php
          $user = get_user_by_id($id);
         ?>
        <input type="text" name="bookId" id="bookId" value=""/>
    </div>
</div>

JS代码是这样的

$('#user').on('show.bs.modal', function (event) {
   var button = $(event.relatedTarget)
   var id = button.data('id')
})

根据上面的 JS 代码,我的用户 ID 在 var id 变量中。 但是,我无法想象如何将上述 PHP 函数的该值用作参数。

有没有可能的方法?

我已将问题编辑为更加独特的 AJAX 方法,其中大多数答案都是基于 AJAX

PS:我对 PHP 和 Bootstrap 很陌生。

【问题讨论】:

  • 你必须通过ajax加载模态内容,一旦页面加载,php不能使用javascript值。
  • 你已经问过这个问题,这里已经回答了:stackoverflow.com/questions/29822406/…
  • 谢谢,我会尝试您的建议...无论如何,我认为这与我之前提出的问题不同。在这里,我更具体地说明了我使用的代码,当我问最后一个问题时,我不知道如何打开一个弹出框。所以,我做到了,另一个问题来了......我在这里...... :-) 无论如何,如果我违反了规则,请告诉我,因为我是新手。

标签: javascript php twitter-bootstrap-3 bootstrap-modal


【解决方案1】:

您将需要使用ajax 来运行 PHP。正如@Tom 所说,javascript 在 DOM 中加载后无法访问 PHP。 PHP 是一种服务器端语言,只能这样访问。

一旦您通过 ajax 连接到 PHP,您就可以使用返回的内容加载 div。只有在加载内容之后才打开模态框,否则您会看到框出现跳动,然后内容才出现。

基本示例

$(".btn").click(function(){
    // AJAX code here.
    $.ajax(
    ....
    success: function($data){
      $('.target').html(data)
      $("#myModal").modal('show');
    }
    );
});

【讨论】:

    【解决方案2】:

    那样你想解决这个问题是不可能的。因为要在 PHP 中从服务器获取数据,您必须向服务器请求,而最好的可能唯一方法是提交表单。在这里,您想以模式显示数据,因此您需要异步执行。所以你可以这样尝试-

        //keep a button to show the modal for user details
        <button class="detail" data-toggle="modal" data-target="#user_modal" data-id="<?= $user->id ?>">Detail</button>
    
        //and using jQuery on the click of the above button post a form to your desire url
            $(document).on("click", ".detail", function () {
                var id = $(this).data('id');
                 $.ajax({
                        type: "POST",
                        url: "your/url",
                        data: { id: id},
                        success: function(data) {
       //and from data you can retrive your user details and show them in the modal
    
                        }});
                     });
    

    【讨论】:

    • 谢谢,我必须说,我以前从未在 AJAX 上工作过。您能否再解释一下上面的代码...我的意思是,我应该在your/url 中输入什么。有什么例子吗?
    • 我很高兴解释它。 :) 在 url 中,您应该将表单操作 url 放在要发布表单的位置。假设,如果您想在 index.php 文件中发布您的表单,那么您可以输入url:"index.php"。如需更多帮助,您可以关注此tuts
    【解决方案3】:

    引导模式允许您加载远程页面,我相信这就是您要寻找的...

    <a href="userdata.php?uid=<?php echo $user['id']?>" data-target="#user" data-toggle="modal"  data-id="{$user['id']}">UserName</a>
    

    那么模态就变成了:

    <div class="modal fade" id="user" tabindex="-1" role="dialog" aria-labelledby="user" aria-hidden="true"><div class="modal-dialog modal-lg"><div class="modal-content"></div></div></div>
    

    还有userdata.php

    <?php
    $user_id = $_GET['uid'];
    $user = get_user_by_id( $user_id );
    ?>
    
    <div class="modal-header">
        HEADER STUFF GOES HERE
    </div>
    
    <div class="modal-body">
        USER INFO GOES HERE
    </div>
    
    <div class="modal-footer">
        MODAL FOOTER HERE
    </div>
    

    【讨论】:

    • 其实我是这样做的。但是,后来我删除了它,因为人们可以在新标签中打开链接。因此,模型不会弹出,只会打开远程文件。太丑了……
    • 您使用的是什么版本的 Bootstrap?这个精确的设置适用于版本 3.2.x
    • 我通过 CDN 使用 3.2.0
    【解决方案4】:

    Bootstrap modal 有一个选项remote,但在 Bootstrap v3.3.0 上已弃用,并将在 v4 中删除。您可以使用jQuery.load。 例如

    $( "#some-click" ).on('click', function() {
        var userId = button.data('id'); //get user id here
        //jQuery.load set html with modal and user details by id  
        $( "#user" ).load( "/not-here.php", { id:userId });
    });
    

    【讨论】:

    • 其实我是这样做的。但是,后来我删除了它,因为人们可以在新标签中打开链接。因此,模型不会弹出,只会打开远程文件。太丑了。
    • 这项工作作为 ajax 请求我只给你解决方法。我评论中的第二个链接为您提供了引导程序的解决方案。我的示例适用于 3x 和 4x 版本,因为引导程序说使用 jQuery.load
    • 在将内容设置为modal-body后,您可以用作引导模式来显示和发送ajax请求
    • 好的,我去看看试试..谢谢:)
    猜你喜欢
    • 2023-04-10
    • 2019-03-24
    • 2016-01-03
    • 2020-07-08
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多