【发布时间】:2016-02-13 23:05:56
【问题描述】:
所以我有一个表单,用户可以在其中输入值并将它们放入数据库中。我正在使用 MVC 架构。 现在,我如何才能在将值发送到数据库的同时制作 PDF。如何使变量在 PDF 文件中工作。
<form action="index.php" method="post" id="make_order_form">
<input type="hidden" name="action" value="make_order">
<input type="hidden" name="action" value="create_PDF">
make_order 将值发送到数据库中,create_pdf 应该制作 pdf 文件
function get_customers() {
global $db;
$query = 'SELECT * FROM customers
ORDER BY customerID';
$statement = $db->prepare($query);
$statement->execute();
return $statement;
}
<?php
function create_customer($fname,$lname,$mobile,$email,$password1,$password2) {
global $db;
$query = 'INSERT INTO customers
(fname,lname,mobile,email,password1,password2)
VALUES
(:fname,:lname,:mobile,:email,:password1,:password2)
'
;
$statement = $db->prepare($query);
$statement->bindValue(':fname', $fname);
$statement->bindValue(':lname', $lname);
$statement->bindValue(':mobile', $mobile);
$statement->bindValue(':email', $email);
$statement->bindValue(':password1', $password1);
$statement->bindValue(':password2', $password2);
$statement->execute();
$statement->closeCursor();
}
这是我在 create_PDF 操作的索引中的内容,我很困惑我应该如何使该值在 pdf 文件中可用。
else if ($action == 'createPDF.php') {
$fname = filter_input(INPUT_POST, 'fname');
$lname = filter_input(INPUT_POST, 'lname');
$mobile = filter_input(INPUT_POST, 'mobile');
$email = filter_input(INPUT_POST, 'email');
$password1 = filter_input(INPUT_POST, 'password1');
$password2 = filter_input(INPUT_POST, 'password2');
$series = filter_input(INPUT_POST,'series');
$bodyType = filter_input(INPUT_POST, 'bodyType');
$trim = filter_input(INPUT_POST, 'trim');
$paint = filter_input(INPUT_POST, 'paint');
$brakes = filter_input(INPUT_POST, 'brakes');
$body = filter_input(INPUT_POST, 'body');
$media = filter_input(INPUT_POST, 'media');
if ($fname == NULL || $fname == FALSE || $lname == NULL || $lname == FALSE || $mobile == NULL || $mobile == FALSE || $email == NULL || $email == FALSE || $password1 == NULL || $password1 == FALSE || $password2 == NULL || $password2 == FALSE || $series == NULL || $series == FALSE || $bodyType == NULL || $bodyType == FALSE || $trim == NULL || $trim == FALSE || $paint == NULL || $paint == FALSE || $brakes == NULL || $brakes == FALSE || $body == NULL || $body == FALSE || $media == NULL || $media == FALSE) {
$error = "Invalid product data. Check all fields and try again.";
include('./errors/error.php');
} else {
get_customers();
}
}
【问题讨论】:
-
我会考虑使用 jsPDF 之类的东西在客户端执行此操作。
-
看起来您可以在
get_customers()中启动 pdf 创建过程,并将$fname, $lname etc...作为参数传递给它,您可以使用它来制作 pdf,如果您也可以将其插入到您的数据库中想要。 -
此页面声称是一种使用 tcpdf 设置 pdf 的经过测试的方法:stackoverflow.com/questions/18223743/… 您可以将其放入您的函数中
-
您还可以创建一个单独的函数
make_pdf($fname,$lname,$mobile,$email,...);,在get_customers();之后调用该函数并将代码放入其中。本质上,您正在制作一个包含变量的 HTML 表格并将其打印到您的 pdf 中,然后您可以做您喜欢的事情 - 保存、下载、显示等。@shankar-damodaran 给出的答案似乎相当清楚,所以我不会复制它,除非你真的需要澄清。
标签: php database html pdf model-view-controller