我有一个PHP代码,可以从数据库中创建一个列表。我从数据库中检索的项目之一是行ID。我在以下代码中创建列表:
<th>Cover Name</th>
<th>Sum Insured</th>
<th>Info</th>
<th style="width: 3.5em;"></th>
</tr>
</thead>
<tbody>
<?php while($row = mysql_fetch_array($query_set)) { ?>
<tr>
<td><?php echo $row['cover_name'] ?></td>
<td><?php echo 'R '.$row['sum_insured'] ?></td>
<td><?php echo $row['info'] ?></td>
<td>
<a href="cover-type.php?id=<?php echo $row['coverid']?>"><i class="fa fa-pencil"></i></a>
<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal"><i class="fa fa-trash-o"></i></a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
调用#myModal
<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal"><i class="fa fa-trash-o"></i></a>
你可以看到主播#myModal
和onclick
点击锚点后,我想将$coverid
传递给以下myModal弹出窗口
<div class="modal small fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Delete Confirmation</h3>
</div>
<div class="modal-body">
<p class="error-text"><i class="fa fa-warning modal-icon"></i>Are you sure you want to delete the cover?<br>This cannot be undone.</p>
</div>
<div class="modal-footer">
<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cancel</button>
<a href="delete-cover.php?id=<?php echo $coverid ?>" class="btn btn-danger" >Delete</a>
</div>
</div>
</div>
</div>
我觉得我需要在
中创建一些隐藏的javascript变量<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal"><i class="fa fa-trash-o"></i></a> and pass it to myModal window, but how do I do that ?
答案 0 :(得分:3)
data-id="< ?php $coverid = $row['coverid'] ?>"
class="trash"
id="modalDelete"
和JQ:
// on clik trash icon
$('.trash').click(function(){
//get cover id
var id=$(this).data('id');
//set href for cancel button
$('#modallCancel').attr('href','delete-cover.php?id='+id);
})
答案 1 :(得分:0)
更改自
<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal">
<i class="fa fa-trash-o"></i>
</a>
到
<a href="#myModal" class="trash" data-id="<?php echo $row['coverid']; ?>" role="button" data-toggle="modal">
<i class="fa fa-trash-o"></i>
</a>
在你的模态中,你会得到这样的id(例如):
<input id="feed_id" value="" />
添加:
$(document).ready(function () {
$('body').on('click', '.trash',function(){
document.getElementById("feed_id").value = $(this).attr('data-id');
console.log($(this).attr('data-id'));
});
});
}
https://torbjornzetterlund.com/passing-data-list-bootstrap-modal/
答案 2 :(得分:-1)
在客户端上执行JavaScript时,PHP在服务器上执行。这段代码
<a href="#myModal" onclick="<?php $coverid = $row['coverid'] ?>" role="button" data-toggle="modal">...</a>
实际上会变成
<a href="#myModal" onclick="" role="button" data-toggle="modal">...</a>
因为PHP部分没有回应任何内容。
所以,你宁愿这样:
<a href="#myModal" onclick="show_dialog('<?php echo $row['coverid']; ?>')" role="button" data-toggle="modal">...</a>
假设你定义了PHP变量$row
而且$row['coverid']
是1337,那么这个HTML将呈现给浏览器:
<a href="#myModal" onclick="show_dialog(1337)" role="button" data-toggle="modal">...</a>
因此,如果您定义JavaScript函数show_dialog
,则会使用$row['coverid']
中的值调用它。将变量从PHP传递到JavaScript的唯一方法是在有JavaScript代码的地方用PHP回显它们的值。
一个好的做法就是在页面顶部的某处使用<script>
块,其中包含以下内容:
window.php_vars = window.php_vars || {};
window.php_vars.some_value = '<?php echo $some_value; ?>';
window.php_vars.some_other_value = '<?php echo $some_other_value; ?>';
window.php_vars.foo = '<?php echo 'bar'; ?>';
...但这种方法仅适用于标量类型(整数,字符串等)。