在 html table 中显示 $_POST 数据

Display $_POST data in html table

我准备了一个表格,用于生成PDF,然后下载提交数据的文件。在我的代码中,我需要通过 mPdf 库来完成,它生成的文件如下:

$mpdf = new \Mpdf\Mpdf();
// Write some HTML code:
$html = "Here comes what must be displayed in the PDF file"
$mpdf->WriteHTML($html);
// Output a PDF file directly to the browser
$mpdf->Output('ticket.pdf', \Mpdf\Output\Destination::DOWNLOAD);

在我的 $html 中,我需要创建一个 table 来显示通过 $_POST 获得的数据的组合,例如但是,当我像下面这样输入时,我有很多错误。

$html = "<table>ID: $_POST['someVariable']</table>";

你可以在下面找到我用来生成文件的完整代码,然后下载它。你能帮我解决这个问题吗?非常感谢您对菜鸟的帮助:)

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $from = filter_input(INPUT_POST, 'from');
    $to   = filter_input(INPUT_POST, 'to');
    $start = filter_input(INPUT_POST, 'start');
    $price = filter_input(INPUT_POST, 'price');
    $length = filter_input(INPUT_POST, 'length');
    $timezoneFrom = getTimezone($from, $airports);
    $timezoneTo = getTimezone($to, $airports);

    validate($from, $to, $start, $price, $length);
    echo "from " . $timezoneFrom . "<br>";
    echo "to " . $timezoneTo . "<br>";

    $dateFrom = new DateTime($start, new DateTimeZone($timezoneFrom));
    echo $dateFrom->format('Y-m-d H:i:s e') . "<br>";
    $dateTo = clone $dateFrom;
    echo "clone of date :";
    echo $dateTo->format('Y-m-d H:i:s e') . "<br>";
    $dateTo->setTimezone(new DateTimeZone($timezoneTo));
    echo "after timezone change:";
    echo $dateTo->format('Y-m-d H:i:s e') . "<br>";
    $dateTo->modify($length . ' hours');
    echo $dateTo->format('Y-m-d H:i:s') . "<br>";
    echo "Data OK";

    }

    require_once __DIR__ . '/vendor/autoload.php';

    $mpdf = new \Mpdf\Mpdf();

     // Write some HTML code:
    $html = "
    <!DOCTYPE html>
    <html>
    <head>
    <style>
        #head {
            text-align: center;
            color: red;
            font-weight: bold;
        }
        table, th, tr, td {
            border: 1px solid black;
        }
    </style>
        <body>
            <table>
                <thead>
                    <tr>
                        <td colspan=\"2\" id=\"head\">SomeTitle</td>
                    </tr>
                </thead>
                    <div class=\"fromTo\">
                        <tr>
                            <td></td>
                            <td>To: ."$_POST['from']".</td>
                        </tr>
                    </div>
                    <div class=\"flightData\">
                        <tr>
                            <td>Departure (local time): $start</td>
                            <td>Arrival (local time)</td>
                        </tr>
                    </div>
                    <div class=\"date\">
                        <tr>
                            <td>$dateFrom</td>
                            <td>$dateTo</td>
                        </tr>
                    </div>
                    <div class=\"FlightPass\">
                        <tr>
                            <td>Flight time:</td>
                            <td>$length</td>
                        </tr>
                        <tr>
                            <td>Passenger:</td>
                            <td></td>
                        </tr>
                    </div>
            </table>
        </body>
    </head>
    </html>";

    $mpdf->WriteHTML($html);

// Output a PDF file directly to the browser
$mpdf->Output('ticket.pdf', \Mpdf\Output\Destination::DOWNLOAD);

如果您有任何建议和问题,请随时向我提供建议和问题。 问候。

你应该永远不要将用户输入直接传递给HTML(甚至不要HTML传递给PDF输入,以保持最佳实践并支持代码的可重用性)因为您的代码将容易受到 XSS 攻击。您应该使用 htmlspecialchars 函数正确处理输入。

$someVariableOutput = htmlspecialchars($_POST['someVariable']);

关于你原来的问题:

您需要使用 . 运算符将字符串与变量连接起来:

$html = "<table>ID: " .  $someVariableOutput . "</table>";

或者您可以在双引号字符串中显示变量:

// the {} are just for a good measure here
// they would be useful when printing eg. an array key.
$html = "<table>ID: {$someVariableOutput}</table>";