duanlu4371 2019-04-24 21:09
浏览 84

在现有页面上使用AJAX调用加载新视图时,如何在其中显示现有的php变量?

In CodeIgniter, I have a web page which is displaying clients. A certain view clients.php is listing clients in a table. It displays the clients like this:

<?php 
foreach ($clients as $client) {
?>
<tr>
    <td><?= $client->companyname ?></td>
    <td><?= $client->city ?></td>
    <td><?= $client->country ?></td>
    <td><?= $client->vat ?></td>
    <td><? <button class="edit-button" data-entityid="<?= $client->id ?>">edit</button></td>
</tr>
<?php
}
?>

As you can see above, I have an edit button for each client. Clicking on a button does an AJAX call as shown below:

$('.edit-button').click(function(){
    $.ajax(
        {
            type: 'POST',
            url: 'modalloader/editclient/' + this.dataset.entityid,
            beforeSend: function(){
                $('.modal-content').html('loading...');
            },
            success: function(result){
                $('.modal-content').html(result);
            },
            error: function (data) {
                $('.modal-content').html('failed');
            }
        });
});

Right now, I am loading the data of the specific client again from the database through my model when I call the method modalloader/editclient/clientid, but in fact I have this data already available on the page (because of the view clients.php). Therefore I was wondering if I can instead grab it from the object $clients which I already have on my web page.

Right now, if I do a var_dump($clients); on the view that I loaded dynamically with the AJAX call to modalloader/editclient/clientid it's showing a null object.

  • 写回答

1条回答 默认 最新

  • drctyr2869 2019-08-27 17:29
    关注

    You can return $clients variable from controller like json and after that set it to js variable. Like this:

    <script>
        var clients = <?php echo $clients ?>;
    <script>
    

    After that you can use this variable in your script

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用