Javascript 如何将变量从引导模式传递到PHP?
我希望在打开模式时将数据id参数的值作为变量传递。我找到了许多与我的主题相关的答案,但我对JS是新手,无法实现任何解决方案Javascript 如何将变量从引导模式传递到PHP?,javascript,php,variables,bootstrap-4,bootstrap-modal,Javascript,Php,Variables,Bootstrap 4,Bootstrap Modal,我希望在打开模式时将数据id参数的值作为变量传递。我找到了许多与我的主题相关的答案,但我对JS是新手,无法实现任何解决方案 <a data-toggle="modal" href="#showcontent" data-id="file1.html">Open1</a> <a data-toggle="modal" href="#showcontent" data-id="file2.html">Open2</a> <div class="
<a data-toggle="modal" href="#showcontent" data-id="file1.html">Open1</a>
<a data-toggle="modal" href="#showcontent" data-id="file2.html">Open2</a>
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php echo file_get_contents($variable); ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
情态标题
&时代;
保存更改
接近
如何将数据id参数的值传递给我的PHP脚本?感谢您的提示,我找到了一个可行的解决方案: 模态:
<div class="modal fade" id="empModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Content</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
&时代;
内容
接近
链接到模式:
<?php
$id = "file1.html";
echo '<a data-toggle="modal" href="" class="showfile" data-id="'.$id.'">Open</a>';
?>
JS:
$(文档).ready(函数(){
$('.showfile')。单击(函数(){
var fileid=$(this).data('id');
$.ajax({
url:'ajaxfile.php',
键入:“post”,
数据:{fileid:fileid},
成功:功能(响应){
$('.modal body').html(响应);
$('empModal').modal('show');
}
});
});
});
以及ajaxfile.php
<?php
$fileid = $_POST['fileid'];
echo file_get_contents($fileid);
exit;
多亏了你的提示,我找到了一个有效的解决方案:
模态:
<div class="modal fade" id="empModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Content</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
&时代;
内容
接近
链接到模式:
<?php
$id = "file1.html";
echo '<a data-toggle="modal" href="" class="showfile" data-id="'.$id.'">Open</a>';
?>
JS:
$(文档).ready(函数(){
$('.showfile')。单击(函数(){
var fileid=$(this).data('id');
$.ajax({
url:'ajaxfile.php',
键入:“post”,
数据:{fileid:fileid},
成功:功能(响应){
$('.modal body').html(响应);
$('empModal').modal('show');
}
});
});
});
以及ajaxfile.php
<?php
$fileid = $_POST['fileid'];
echo file_get_contents($fileid);
exit;
什么是“参数值”?您只需要Javascript/HTML将这些数据id参数传递给PHP脚本。您可以使用HTML表单或AJAX调用,但不能像那样使用PHP动态更改模式的内容。您需要将一个单击处理程序添加到
标记中,该标记从服务器获取内容并更新模态体。因为您使用的是Bootstrap,所以可以使用jQuery库。什么是“参数值”?您只需要Javascript/HTML将这些数据id参数传递给PHP脚本。您可以使用HTML表单或AJAX调用,但不能像那样使用PHP动态更改模式的内容。您需要将一个单击处理程序添加到
标记中,该标记从服务器获取内容并更新模态体。因为您使用的是引导,所以可以使用jQuery库。