将PHP变量传递给HTML链接以进行模式和AJAX [英] Passing PHP variable to Html link for modal and AJAX
问题描述
在CakePHP 3中,烘焙的控制器具有4个具有各自视图的函数:索引,添加,编辑和删除.我试图将编辑"和查看"功能移至模态,但是我需要PHP变量来确定要修改或查看的特定数据条目.
In CakePHP 3, baked controllers have 4 functions that have their own views: Index, Add, Edit and Delete. I'm trying to move the Edit and View functions to modals, but I need the PHP variable to determine the specific data entry that is to be modified or viewed.
例如.我有一个名为用户"的表.
Eg. I have a table called Users.
在UsersController的Index函数中,我将定义以下变量:
In the Index function of the UsersController, I would define the following variable:
$users = $this->paginate($this->Users);
$this->set(compact('users'));
$this->set('_serialize', ['users']);
然后在表内的索引视图(Index.ctp)上,我将执行以下操作:
Then on the Index view (Index.ctp), inside the table I would have the following actions:
<?php foreach ($users as $user): ?>
<tr>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $user->id]) ?>
</td>
</tr>
<?php endforeach; ?>
我希望以上操作将$ user-> id传递到HTML标记中,该标记将打开如下所示的模式:
I want the above actions to pass $user->id into a HTML tag that would open up a modal as follows:
查看 编辑
并使用JavaScript打开模型.
And use JavaScript to open up the model.
$(function () {
$('#view').click(function () {
$('#viewModal').modal('show');
});
});
$(function () {
$('#edit').click(function () {
$('#editModal').modal('show');
});
});
或者,只需将其直接传递给模式,然后基于该模式为每个表单字段获取数据.
Alternatively, just pass it straight to the modal, and then fetch data for each of the form fields based on that.
然后对于编辑"模式,保存时,我将使用AJAX PUT进行保存.
Then for the Edit modal, when saving, I would use AJAX PUT to save.
编辑模式:
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myModalLabel">Edit</h4>
<div>
<div class="col-sm-6">
<?= $this->Form->input('firstname', ['class' => 'form-control', 'label' => 'First Name', 'id' => 'firstname']); ?>
</div>
<div class="col-sm-6">
<?= $this->Form->input('lastname', ['class' => 'form-control', 'label' => 'Last Name', 'id' => 'lastname']); ?>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close
</button>
<button id="savebutton" type="button" class="btn btn-primary" data-dismiss="modal">Save changes
</button>
</div>
</div>
</div>
</div>
推荐答案
将ID传递给您的链接,如下所示:
pass an id to your link as follows:
<?php foreach ($users as $user): ?>
<tr>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $user->id]), ['id' => 'view', 'data-id' => $user->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'view', $user->id]), ['id' => 'edit', 'data-id' => $user->id]) ?>
</td>
</tr>
<?php endforeach; ?>
然后在模态中按如下所示定义用户ID:
then within your modals define user id as follows:
尚不清楚您如何实现模态,所以我将假设以下内容
$(function () {
$('#view').click(function (ev) {
ev.preventDefault();
var userId = $(this).attr('data-id');
$('#viewModal').modal('show');
});
});
$(function () {
$('#edit').click(function (ee) {
ee.preventDefault();
var userId = $(this).attr('data-id');
$('#editModal').modal('show');
});
});
您将需要将用户名传递给您使用的模态
you will need to pass the userid to your modal you using
这篇关于将PHP变量传递给HTML链接以进行模式和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!