Transfer dynamic data to boot modal

I am trying to upload comments of a specific post by modal. To do this, I need to pass the message identifier to modal, and then get the corresponding comments. The modal starts as follows:

<a class="xyz" data-toggle="modal" data-target="#compose-modal" data-id="<?php echo $list[$i]->getID(); ?>">View Comments</a> 

And modal is defined at the bottom of the page as follows:

 <div class="modal fade" id="compose-modal" tabindex="-1" role="dialog" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <!-- here i need to use php to fetch the comments using post id --> </div> </div> </div> 
+1
source share
2 answers

PHP runs until the page returns to the browser. When you see the page in your browser, all PHP is already executed. What you probably want to do is use AJAX. Here is a general description of how you will do this:

You have a PHP page that takes an identifier and returns the data you need as JSON.

api.php

  $theId = $_POST['theId']; //Get the information you want, and turn it into an array called $data header('Content-Type: application/json'); echo json_encode($data); 

In your html, you should run the modal version using the onclick binding to View Comments:

 <a class="xyz" onclick = "launch_comment_modal(<?php echo $list[$i]->getID(); ?>)">View Comments</a> 

then below with your other javascript:

  <script> $('#compose-modal').modal({ show: false}); function launch_comment_modal(id){ $.ajax({ type: "POST", url: "api.php", data: {theId:id}, success: function(data){ //"data" contains a json with your info in it, representing the array you created in PHP. Use $(".modal-content").html() or something like that to put the content into the modal dynamically using jquery. $('#compose-modal').modal("show");// this triggers your modal to display }, }); } </script> 
+5
source

Just pass the content via ajax using php page and echo content in modal

0
source

Source: https://habr.com/ru/post/1240239/


All Articles