在提交时打开thankyou.html+下载PDF

On Submit open the thankyou.html + download the PDF

本文关键字:html+ 下载 PDF thankyou 提交      更新时间:2023-09-26

我有一个简单的下载表单,在提交时,我希望用户能够下载PDF并同时打开感谢页面。

我尝试过使用window.open和一些PHP方法(头)。当PHP方法弹出下载pdf屏幕时,页面不会重定向到感谢页面。

有人能帮助JS或PHP吗?

我对这两个都不太了解——如果你能分享整个代码,我会非常感激。谢谢!:)

最好的方法是正常打开感谢表单,然后在超时时(使用setTimeout),使用window.open("url", "_blank")打开PDF下载。

您需要3个文件:

  1. 您的文件和表单页面具有:
    <form action="thanks.php" id="form" method="POST"><!-- your form content and a submit button here--></form>

  2. 感谢.php页面,该页面将有一个:
    <iframe src="download_pdf.php" style="display:none;" />

  3. 最后创建download_pdf.php页面:
<?php
 $file = 'filename.pdf';
 if(!file){
     die('Error: file not found');
 }else{
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$file");
     header("Content-Type: application/pdf");
     header("Content-Transfer-Encoding: binary");
     readfile($file);
 }
 ?>

在将任何其他内容发送到浏览器之前,您只能使用header()进行PHP重定向。页面加载后,您将需要使用JS。这对JS:来说很简单

window.location = "http://example.com";

然而,我会质疑同时加载两个窗口。显示感谢页面并提供一个链接可能会更容易,他们可以在该页面上查看/下载PDF。

实际上,用户不应该重复使用感谢页面url来重复下载pdf,这将首先破坏获取用户信息的目的。因此,在可以显示下载的感谢页面中,超时是必要的。但检查一下你是否可以通过使用弹出窗口来屏蔽感谢页面的url。