使用AJAX在Bootstrap Modal上调用PHP函数 [英] Call a PHP function on a Bootstrap Modal using AJAX

查看:75
本文介绍了使用AJAX在Bootstrap Modal上调用PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PHP循环在我的页面上回显的用户列表。当我单击每个用户的名称时,会弹出Bootsrap Modal并显示用户的更多详细信息。链接看起来像这样。

I have a list of users echoed on my page using a PHP loop. When I click each user's name, a Bootsrap Modal pops up and display further details of the user. The links look like this.

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

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

As you can see, I'm passing user's ID to the Bootstrap modal so that I can use it to retrieve other information of the user by calling get_user_by_id() in the Bootstrap Modal.

模态看起来像这样

<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代码,我在 var id 变量中有我的用户ID。
但是,我无法想象如何将上述PHP函数的值用作参数。

According to the above JS code, I have my user's ID in var id variable. But, ther's no way I can imagine how to use that value for the above PHP function as the argument.

有没有可能的方法呢?

Is there any possible way for that?

我已经编辑了一个关于AJAX方法更独特的问题,其中大多数答案都基于AJAX

I have edited the question to be more unique about AJAX methods which most answers were based on AJAX

PS :我是PHP和Bootstrap的新手。

PS: I'm very new to PHP and Bootstrap.

推荐答案

您将需要使用 ajax 。由于@Tom声明javascript在DOM中加载后无法访问PHP。 PHP是一种服务器端语言,只能这样访问。

You will want to use ajax to run the PHP. As @Tom states javascript cannot access PHP after the loading in the DOM. PHP is a server side language and can only be accessed as such.

一旦通过ajax连接到PHP,就可以加载 div 包含返回的内容。只有在加载内容后,如果您打开模式,否则您将看到显示该框的内容,然后显示内容。

Once you connect to the PHP through ajax you can then load the div with the content from the return. Only AFTER the content is loaded should you open the modal otherwise you will have a jumpy look of the box showing up and then content just appearing.

基本示例

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

这篇关于使用AJAX在Bootstrap Modal上调用PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆