How to place output from an external php file to a specific place on the page
css, html, php
Solution
<?php
if (isset($_GET['outputHtml']))
{
echo "<p class='commentBox'>" . "This is from external PHP" . "</p>";
echo "<p class='commentBox'>" . "This is also from external PHP" . "</p>";
include 'writephp.php';
}
This in your write.php is not correct, see `writephp.php` is a complete HTML page with `<html>` `<head>` and `<body>` tags and you are including it after echoing some `<p>` tags, that won't make your new boxes appear magically inside the body of the page, you would be creating some invalid markup with the two `<p>` at the beginning of the file followed by the `<!DOCTYPE><html>` etc...
EDIT
Regarding the update to your question that now reads "How place output from an external php file to a specific place on the page"
As @Robert Seddon-Smith correctly noted in his answer, using `include` will make the content appear exactly at the point when the call is made, so if you wanted to include your new boxes in your main file then the call to `include` should be backwards, that is, you should include the content inside the main file, you can use this example to test it:
Modify `write.php` to just echo the boxes when the $_GET variable is present
<?php
if (isset($_GET['outputHtml']))
{
echo "<div class='commentBox'>" . "This is from external PHP" . "</div>";
echo "<div class='commentBox'>" . "This is also from external PHP" . "</div>";
}
?>
And make the include call in your main file `writephp.php`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test Php </title>
<link rel="stylesheet" href="writephp.css">
</head>
<body>
<div id="allContent">
<?php for($i=0; $i<3; $i++): ?>
<div class="commentBox">
<p> Just a box</p>
</div>
<?php endfor; ?>
<?php include 'write.php'?>
<a href="writephp.php?outputHtml">Php output</a>
</div>
</body>
</html>
This will include your two new boxes inside the container when you click in the link.
Also you will note that there is nothing special about styling content from an included file, that is because in the end all the markup is rendered as a single HTML page, it doesn't matter if the content is echoed from the main page or the included file, it will obey any rules in your CSS
Problem
I have looked at other posts here regarding similar questions but cannot find an answer. Edit I have been playing with this and I want to alter my question somewhat. I am having a problem placing the output that is echoed from an external php file. Here is some example code to demonstrate my problem - this is my main file `writephp.php`: ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Test Php </title> <link rel="stylesheet" href="writephp.css"> </head> <body> <div class="allContent"> <?php for($i=0; $i<3; $i++): ?> <div class="commentBox"> <p> Just a box</p> </div> <?php endfor; ?> <a href="write.php?outputHtml">Php output</a> </div> </body> </html> ``` Now I can center this with css: ``` .allContent { width: 400px; margin-left: auto; margin-right: auto; font-family: Arial; } .commentBox { border: 1px solid black; width: 400px; padding: 0px 5px 5px 5px; margin-top: 5px; } ``` So in the html file the php writes out boxes which are centered in the page since the php loop is within the "allConent" div. The anchor envokes an external php file which will echo some more boxes. That file looks like this and is called `write.php` (`writephp.php` is the main file this is called from): ``` <?php if (isset($_GET['outputHtml'])) { echo "<p class='allContent commentBox'>" . "This is from external PHP" . "</p>"; echo "<p class='allContent commentBox'>" . "This is also from external PHP" . "</p>"; include 'writephp.php'; } ``` But the echoed output from the external php is above the output from the main file -in fact it is placed before the `<!doctype html>` tag , obviously not good. the page source looks like this: ``` <p class='commentBox allContent'>This is from external PHP</p ><p class='allContent commentBox'>This is also from external PHP</p> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Test Php </title> <link rel="stylesheet" href="writephp.css"> </head> <body> <div class="allContent"> <div class="commentBox"> <p> Just a box</p> </div> <div class="commentBox"> <p> Just a box</p> </div> <div class="commentBox"> <p> Just a box</p> </div> <a href="write.php?outputHtml">Php output</a> </div> </body> ``` In general my question would be "how do I place html coming from an external php file anywhere I want on the page? "