How to insert an HTML file into a PHP variable?

Asked

Viewed 915 times

0

Fala galera,

I have a file called post_forms.phpthat picks up the results of $_POST[], must fill variables inside the file form_xxx.html, create a PDF with Dompdf lib and send with Phpmailer.
See a part of each file:

post_forms.php

setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "pt_BR", "pt_BR.iso-8859-1", "pt_BR.utf-8", "portuguese"); 
date_default_timezone_set('America/Bahia');
$data_cadastro = utf8_encode(ucfirst(strftime('%A, %d/%m/%Y, %H:%M:%S')));
$tipo_form = $_POST['tipo-form'];
$protocolo = 'DA'.date("Ymdis");

switch ($tipo_form)
{
    case"Form A":
    ob_start();
    include("forms/form_A.html");;
    $html = ob_get_clean();
    break;
}

echo $html;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();

form_A.html

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
</head>
<body>
    <main>
        <table class="table table-bordered" style="text-align:center;">
            <tr>
                <th rowspan="2" align="center"><img src="img/logo.png"></th>
                <th colspan="4" style="font-size: 1.5em;">FORMULÁRIO - Formulário Tipo A</th>
            </tr>
            <tr>
                <th colspan="2"><strong>$tipo_form</strong> - <big>$protocolo</big></th>
                <th colspan="2"> $data_cadastro</th>
            </tr>
            <tr>
                ...
            </tr>
            <tr>
                ...
            </tr>
            <tr>
                ...
            </tr>
            <tr>
                ...
            </tr>
        </table>
    </main>

    <script> ... </script>
</body>
</html>  

I have no problem with the Dompdf classes and even with Phpmailer, I can generate the pdf and send by email.
What I need to know is, why the PHP variables that are inside the html file are being written with the String containing the name of the Ex variable: $data_cadastro and is not being printed the content of the variable that would be by Ex: Saturday, 08/09/2018, 11:34:00

  • 1

    Friend tries the following.... change line <th colspan="2"><Strong>$type_form</Strong> - <big>$protocol</big></th> <th colspan="2"><? echo $data_registration? > </th> for <th colspan="2"><Strong>$type_form</Strong> - <big><?= $protocol? ></big></th> <th colspan="2"><? echo $data_cadastre? ></th>

  • 1

    See the answer: https://answall.com/questions/7703/utilizo-php-dentro-de-um-html-ou-um-html-dentro-de-um-php

1 answer

1


I found the problem, the tags were missing <?php ?> and the echo call in the archive .html

form_A.html

                <th colspan="2"> <?php echo $data_cadastro; ?></th>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.