[英]Passing php variable to modal twitter bootstrap not working
Hi I'm passing php variable to twitter boostrap modal. 嗨,我正在将php变量传递给twitter boostrap modal。 but I cant get the right id when I click the link inside the modal. 但单击模式内的链接时,我无法获得正确的ID。 please see code below. 请参见下面的代码。 first I have created a foreach function where data loops second I have created a link to show the modal 3rd I have actions link inside the modal where user can update, delete the data. 首先,我创建了一个foreach函数,其中有数据循环,其次,我创建了一个链接,以显示模式3rd,我在模式内部有动作链接,用户可以在其中更新,删除数据。 what I have achieve already is that I can pass the variable of php going to modal actions, but when I try to other rows it shows the same id still. 我已经实现的是,我可以将php的变量传递给模式操作,但是当我尝试传递给其他行时,它仍然显示相同的id。
<table class="table table-condensed table-bordered table-striped volumes">
<thead>
<tr>
<th>ID</th>
<th>Director</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
foreach ($natcco_members as $nattco) {
$id = $nattco['id'];
echo "<tr>
<td>".++$counter."</td>
<td>".$nattco['director']."</td>
<td><a href='#' class='b'>".$nattco['agent_name']."</a></td>
<td>".$nattco['address']."</td>
</td>";
?>
<!-- Modal HTML -->
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Choose your actions!</h4>
</div>
<div class="modal-body" align="center">
<a href='update_sub_agent.php?id=<?php echo urlencode(htmlspecialchars($id)) ?>' class='btn btn-success'><i class='icon-pencil'></i> Edit</a>
<a href='owner.php?id=".urlencode(htmlspecialchars($id))."' class='btn btn-info'><i class='icon-user'></i> Owner</a>
<a href='fla.php?id=".urlencode(htmlspecialchars($id))."' class='btn btn-warning'><i class='icon-list-alt'></i> FLA</a>
<a href='delete.php?id=".urlencode(htmlspecialchars($id))."' class='btn btn-danger'><i class='icon-remove'></i> Delete</a>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
<?php "</tr>";
}
}
?>
</tbody>
</table>
I have made quite some edits to your code, let's start with what you were doing first: 我已经对您的代码进行了一些编辑,让我们从您首先要做的事情开始:
<td>".$nattco['address']."</td> </td>";
This has to be </tr>
instead of </td>
. 必须是</tr>
而不是</td>
。
You only need one modal box. 您只需要一个模式框。 We will pass the id
for them through jQuery. 我们将通过jQuery传递id
。 So create only 1 static modal box at the end of your table. 因此,在表末尾仅创建1个静态模式框。
Now the things you need to do: 现在您需要做的事情:
1) Make use of this $id = $nattco['id'];
1)利用这个$id = $nattco['id'];
as your <tr id>
so it will be: 作为您的<tr id>
因此它将是:
$id = $nattco['id'];
echo "<tr id='".urlencode(htmlspecialchars($id))."'> #...rest of your code
2) Give a class for your buttons in the modal box: 2)在模式框中为您的按钮提供一个类:
<a href='update_sub_agent.php' class='btn btn-success edit'><i class='icon-pencil'></i> Edit</a>
<!-- ^edit class added -->
<a href='owner.php' class='btn btn-info owner'><i class='icon-user'></i> Owner</a>
<!-- ^owner class added -->
<a href='fla.php' class='btn btn-warning fla'><i class='icon-list-alt'></i> FLA</a>
<!-- ^fla class added -->
<a href='delete.php' class='btn btn-danger delete'><i class='icon-remove'></i> Delete</a>
<!-- ^delete class added -->
3) Add href
and class to your
` tag : 3) class to your
`标签中添加href
和class to your
:
`<td><a href='#myModal' class='b modal-box'>".$nattco['agent_name']."</a></td>`
And finally, the jQuery bit: 最后,jQuery位:
$('a.modal-box').click(function(e){
id=$(this).closest('tr').attr('id');
$('.modal-body .edit').attr('href','update_sub_agent.php?id='+id+'');
$('.modal-body .owner').attr('href','owner.php.php?id='+id+'');
$('.modal-body .fla').attr('href','fla.php?id='+id+'');
$('.modal-body .delete').attr('href','delete.php?id='+id+'');
});
That's all. 就这样。 This minimizes your code so that modal box is not repeated while dynamically passing id
of the clicked row. 这样可以最大程度地减少您的代码,以便在动态传递所单击行的id
时不会重复使用模式框。
You can have a look a the demo here: 您可以在此处查看演示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.