在现有页面上使用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.

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