代码之家  ›  专栏  ›  技术社区  ›  MrSkover

在html表格中显示$u POST数据

  •  0
  • MrSkover  · 技术社区  · 7 年前

    我准备了一个表单,用于生成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中,我需要创建一个表,该表显示了通过$U 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);
    

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Finwe    7 年前

    你应该 从不 直接将用户输入传递到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>";