Open a modal that comes from a different PHP page

0

I want to open a modal on a menu button but only put it in a separate file just for him, I wanted to know what command to open it by pulling from another file in PHP

    
asked by anonymous 05.06.2018 / 19:17

2 answers

1

I use it that way.

  

index.html

 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <button type='button'  onclick='abreModal()' >Abrir</button>

<div class="modal"></div>

    //Ajax
    <script type="text/javascript">
    function abreModal(){
      $.ajax({
        type: 'POST',
        //Caminho do arquivo do seu modal
        url: 'pasta/modal.html',
        success: function(data){              
          $('.modal').html(data);
          $('#myModal').modal('show');
        }
      });
    }
    </script>
Arquivo modal.html
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" width="100%">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <label class="modal-title">Modal</label>
      </div>
      <div class="modal-body">
        <p>Conteudo</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-link waves-effect"  data-dismiss="modal">FECHAR</button>
      </div>
    </div>
  </div>
</div>
    
06.06.2018 / 14:18
0

Iframe is probably the solution (probably combined with .embed-responsive ):

<div class="embed-responsive embed-responsive-16by9">
    <iframe class="embed-responsive-item" src="url-da-pagina.php"></iframe>
</div>

Something like:

<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">
        <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item" src="URL-DA-PAGINA.PHP"></iframe>
        </div>
      </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>

Documentation:

Example:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
  Exibir
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">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">
        <div class="embed-responsive embed-responsive-16by9">
          <iframe class="embed-responsive-item" src="URL-DA-PAGINA.PHP"></iframe>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Opening links in iframe

If you want to open links in the iframe you can do an event with jQuery (bootstrap uses jQuery)

  

Example in jsfiddle: link

like this:

$(document).on('click', 'a[data-toggle]', function (e) {
    e.preventDefault();

    //carrega URL do link no IFRAME
    $("#iframeModal").attr("src", this.href);
});

The HTML would be this:

<!-- Button trigger modal -->
<a href="pagina1.php" data-toggle="modal" data-target="#exampleModal">
  Página 1
</a> |
<a href="pagina2.php" data-toggle="modal" data-target="#exampleModal">
  Página 2
</a>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">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">
        <div class="embed-responsive embed-responsive-16by9">
          <iframe id="iframeModal" class="embed-responsive-item" src=""></iframe>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

Opening links with Ajax ( $().load() )

If you do not need to import the resources (.js and .css) into the IFRAME you can use Ajax, in which case the $().load() function should solve your problem. >

$(document).on('click', 'a[data-toggle]', function (e) {
    e.preventDefault();

    //carrega URL do link com Ajax
    $("#modal-conteudo").load(this.href);
});

Take into consideration that links should only contain HTML, without the <head> , it is just to load within the modal, as if it had been loaded via include (similar but of course different HTTP requests) / p>

The HTML would be this:

<!-- Button trigger modal -->
<a href="pagina1.php" data-toggle="modal" data-target="#exampleModal">
  Página 1
</a> |
<a href="pagina2.php" data-toggle="modal" data-target="#exampleModal">
  Página 2
</a>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div id="modal-conteudo" class="modal-body"></div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
    
05.06.2018 / 19:21