[英]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中,我需要创建一个表,该表显示通过$ _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>";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.