Modal bootstrap does not open as dynamic link

Asked

Viewed 1,109 times

0

I bring a PHP/Mysql listing from which I open a product description in a modal window. The code is as follows:

Page list-products.php

while($jm = mysqli_fetch_object($sql)){
  $listar = "<a href=\"ver-produtos.php?Key=".$jm->IdCodProdutos."\" data-toggle=\"modal\" data-target=\"#myModal\" class=\"btn btn-xs btn-primary\">Ver currículo</a></div>";
}

Modal

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
     <div class="modal-dialog">
           <div class="modal-content">

           </div>
           <!-- /.modal-content -->
     </div>
     <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

Page ver-products.php

// teste
echo $_REQUEST['Key'];

The window opens correctly, but you need to update the product listing page to get the other Key.

1 answer

2


The modal keeps the content of the first request, so the first one you open will be permanent until the page reloads.

To fix the problem create an alternate toggle that redos the load.

$(document).on('click', "[data-toggle='meumodal']", function(event){
  event.preventDefault();
  target = $(this).attr("data-target");
  content = $(this).attr("href");
  $(target+".modal .modal-content").load(content,function(){
     $(target).modal('show');
  });
});

And use in your links

while($jm = mysqli_fetch_object($sql)){
  $listar = "<a href=\"ver-produtos.php?Key=".$jm->IdCodProdutos."\" data-toggle=\"meumodal\" data-target=\"#myModal\" class=\"btn btn-xs btn-primary\">Ver currículo</a></div>";
}
  • Hello Kadu. Right. I made the changes, only the see-products.php link opens in a new tab and not in modal.

  • 1

    Opa @Jose.Marcos. You are using jQuery? You changed PHP by adding the attribute data-toggle=\"meumodal\" in the link?

  • 1

    I updated the code for a better version...

  • Perfect Kadu. Thanks for your help.

Browser other questions tagged

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