我有一个提交给我想要执行以下操作的 php 脚本的表单:
- 通过 POST 捕获用户输入(完成)
- 向我发送一封包含用户详细信息的电子邮件(完成)
- 开始从与 .php 文件 (test.pdf) 相同的目录下载 PDF - 帮助!
编辑:仅供参考,我通过 jquery 调用 php:
$.ajax({
type: "POST",
url: php_url,
data: $('#popForm').serialize(),
success: function(){
window.location.href = 'downloadpdf.php?file=test.pdf';
}
})
这是通过 POST 捕获用户输入并通过电子邮件发送给我的 php 代码。我只需要上面的#3 部分。
<?php
$email_PGi = "me@mail.com";
$email_subject = "some email subject";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email_message = "The following is a new message received via the website:\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($firstname)."\n";
$email_message .= "Last Name: ".clean_string($lastname)."\n";
// create email headers
$headers = 'From: '.$biz_email."\r\n".
'Reply-To: '.$biz_email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_PGi, $email_subject, $email_message, $headers);
?>
下载pdf.php
<?php
$file = $_GET['file'];
header('Content-Type: Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . $file);
readfile($filename);
die();
?>