Open a modal that comes from a different PHP page

Asked

Viewed 4,599 times

1

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

  • But in this separate file only has the modal html code?

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>
    <script src="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>

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 iframe you can do an event with jQuery (bootstrap uses jQuery)

Example in jsfiddle: http://jsfiddle.net/8dumLrcx/2/

thus:

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

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

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())

To not need to import the Resources (.js and .css) within IFRAME you can use Ajax, in this case the function $().load() should solve your problem, should look like this code (is no iframe):

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

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

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

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>
  • page I meant is file is kind a include but it’s in href I don’t know how it does

  • @Brunoalbuquerque I don’t know if I got it right, but if that’s what I got, you’re confusing, PHP is back-end, so include is executed in the back-end, when it arrives at the front-end everything is already processed in HTML format, there’s no way to run a include on the front-endend, sites work with HTTP communication, ie it is client and server, after you downloaded the page PHP has already run, there is no way to go back in time. What you could do is use IFRAME or use Ajax, I’ll give you a short example.

  • <the data-toggle="modal" href="fashionlhorario.php?" > type so but not working out the modal is in a different file

  • 1

    @Brunoalbuquerthat you see the example http://jsfiddle.net/8dumLrcx/2/, just adapt to something like <a href="pagina1.php" data-toggle="modal" data-target="#exampleModal">&#xA; Página 1&#xA;</a> |&#xA;<a href="pagina2.php" data-toggle="modal" data-target="#exampleModal">&#xA; Página 2&#xA;</a>

  • @Brunoalbuquerque I added an example with jQuery’s Ajax (.load) too, see the answer edits

Browser other questions tagged

You are not signed in. Login or sign up in order to post.