contool contool - 8 months ago 42
PHP Question

php include inside a variable to be used later

I'm trying to include a whole page when a form is submitted, which will then be sent by PHPMailer. It looks something like this:

Form1.php :

if (!empty($_POST['email']) {
$variable_email = $_POST['email'];
$body = include (content.php);
Functions::sendEmail($body, $variable_email)


<form action='Form1.php' method=post>
<input name="email"></input>
<button type='submit' />

content.php :

$id = $variable_email

// DB Queries to SELECT stuff based on $id
$db_output = 'information'

<div><?php echo $db_output; ?></div>

When I submit the form it immediately includes content.php in the page and doesn't send the content in the email function.

I've tried going down the ob_start() route as suggested elsewhere, but when I output the get_file_contents() it outputs all content including the php & sql queries. To be clear, my content/email to be sent should only include the html portion.

I've looked at this from a number of angles, I can't seem to get it to work. Should I be approaching it another way perhaps? Ideally I'd just like to output my entire contents.php file into the Function's variable and have it look exactly like it does if I include the same file on an HTML page i.e. is there a way to "prepare" the include statement without executing it immediately?



when using ob_start, you want ob_get_contents (and likely ob_get_clean instead). file_get_contents is for opening and reading a file as text which would get you the source of the file.

In Form1.php:

if (!empty($_POST['email'])) {
    $variable_email = $_POST['email'];

    include 'content.php';
    $body = ob_get_clean();

    Functions::sendEmail($body, $variable_email);