Problem concatenating file contents, in order, into one variable

Viewed 21

I'm trying to create a form on my site where I can type up an email and send to users from my DB - but using a nicely formatted template. I have the header and footer of the HTML file in separate files, however, when I try to add my variable in the center it keeps showing up underneath everything at the bottom of the page.

$bodyNew = include 'mail-template-header.html';
$bodyNew .= '<p>' . $_POST['mytextarea'] . '</p>';
$bodyNew .= include 'mail-template-footer.html';

This is the code I'm using to create the body. It just grabs snippets of the single html template page and in the center is an area where you can drop a paragraph. The output has the template file as intended, but blank where the content is. The variable from the POST shows up at the bottom at the end of the output.

1 Answers

Try with ob_start() and ob_get_clean() to collect the output of an included file. This would have been more relevant when including a php file that has calculated output.

<?php

$html = '';

ob_start();
include 'mail-template-header.html';
$html .= ob_get_clean();

$html .= '<p>' . $_POST['mytextarea'] . '</p>';

ob_start();
include 'mail-template-footer.html';
$html .= ob_get_clean();
?>

Related