True speaker
True speaker

Reputation: 362

How to show this .php file inside this modal div?

Now when I click the link it goes to link. works fine. But how can I show posts.php inside the div(modal) below.

I dont want to leave index.php . If it was a normal .php file I would simply inlcude and it would appear in the modal.

Here simple include does not work cause I need to pass the id in posts.php for getting the exact post.

what should I do ?

index.php

<li data-toggle="modal" data-target="#myModal"><a href="posts.php?id=<?=$post_id?>"  >show post</a></li>





<div id="myModal" class="modal fade" 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">Post</h4>
      </div>
      <div class="modal-body">
        
		<div >
          
          
          
          
          // I need  show posts.php  inside here
		
		
	
          
          
		
		</div>
		
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>

posts.php

<?php 
include("connect.php");


	
$post_id = $_GET['id'];

$get_post = " SELECT * FROM `posts` where post_id='$post_id' ";

$run_post = mysql_query($get_post);
	
while ($row=mysql_fetch_array($run_post)){
	
	
		$post_title = $row['post_title'];
		
		$post_image = $row['post_image'];
		
	}

?>


<p> <?php echo $post_title;?></p>

<img  src="images/<?php echo $post_image;?>" />

Upvotes: 2

Views: 1140

Answers (1)

Prime
Prime

Reputation: 2482

You basically have three choices here:

include/require

You can use the keywords include or require to tell PHP to basically run the contents of another PHP file in the current file, like so:

<?php

$foo = "bar";

include "othercode.php"; //or alternatively: require "othercode.php";

echo "$foo + $variableinothercode";
?>

The difference between include and require is that require will throw a critical error if the file could not be found, while include will only throw a warning.


iFrame

I wouldn't recommend this option; however it is an option so I'll include it anyway;

You could simply replace the <div> tags with an <iframe> with it's src attribute set to the location of the file, like so:

<iframe src="http://example.com/othercode.php"></iframe>

Copy/cut the code into the file

There's always the option of making the other file redundant by copying or cutting the contents of the file into the new file, however this should only be done if the file is not being included or used anywhere else as it will break those scripts; a simple include or require will suffice and be infinitely cleaner.

Upvotes: 1

Related Questions