简体   繁体   English

如何将变量从引导模式传递到 PHP?

[英]How can I pass a variable from bootstrap modal to PHP?

I'd like to pass the data-id parameter's value as a variable while I open a modal.我想在打开模式时将 data-id 参数的值作为变量传递。 I found many answers related to my topic, but I am new to JS and I can't implement any solution.我找到了许多与我的主题相关的答案,但我是 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="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">&times;</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>

How can I pass the data-id parameter's value to my PHP script?如何将 data-id 参数的值传递给我的 PHP 脚本?

Thanks to your hints, i found a working solution:感谢您的提示,我找到了一个可行的解决方案:

modal:模态:

            <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">&times;</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>

Link to the modal:链接到模态:

                <?php 
                $id = "file1.html";
                    echo '<a data-toggle="modal" href="" class="showfile" data-id="'.$id.'">Open</a>';
                ?>

JS: JS:

            <script type='text/javascript'>
            $(document).ready(function(){
                $('.showfile').click(function(){
                    var fileid = $(this).data('id');
                    $.ajax({
                        url: 'ajaxfile.php',
                        type: 'post',
                        data: {fileid: fileid},
                        success: function(response){ 
                            $('.modal-body').html(response); 
                            $('#empModal').modal('show'); 
                        }
                    });
                });
            });
            </script>

and the ajaxfile.php和 ajaxfile.php

<?php
$fileid = $_POST['fileid'];
echo file_get_contents($fileid);
exit;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM