【问题标题】:using php to show a pdf file?使用 php 显示 pdf 文件?
【发布时间】:2014-09-17 14:42:16
【问题描述】:

我正在尝试使用 php 来检查表单变量,如果所有内容都检查完毕,则向用户呈现一个存储在我的服务器上的 pdf 文档,该文档与我的 php 文件位于同一目录中。我正在使用的代码意味着它会在用户计算机上下载 pdf,但不会自动显示它。我想这样做,如果我的验证通过,用户将被重定向到 pdf 文档。如果我的验证失败,则会向用户显示一条错误消息。

我什至尝试使用 jQuery 发布表单数据,希望它能将 pdf 返回给用户,但这不起作用。

有人知道如何实现我想要的吗?

我的 HTML:

<form name="mileage" action="test.php" method="post">
     <button type="button">Mileage Reimbursement Log</button>
     Email Address: <input type="text" name="email" id="email"/><br>
     <input type="submit" value="Login" title="login"/>
</form>

我的 PHP:

<?php
$myEmail = $_POST['email'];

if($myEmail == "test"){
   header('Content-type: application/pdf');
   header("Content-disposition: attachment; filename=Mileage-Reimbursement-Log.pdf");
   readfile("Mileage-Reimbursement-Log.pdf");
}else{
   echo "sorry";    
}
?>

我尝试过的 jQuery:

$('form').submit(function() {
    if($('#email').val() != ''){
        $.post('test.php', $("form[name=mileage]").serialize(), function(data) {            
        }).error(function(){
            alert("Error submiting the form");
        });
    }else{
        alert("No Email Address");
        return false;
    }
});

【问题讨论】:

  • 据我所知,没有办法使用 AJAX 下载文件。

标签: php jquery forms pdf


【解决方案1】:

不需要 JQuery 代码。用内联替换附件

header('Content-type: application/pdf');
header("Content-disposition: inline; filename=Mileage-Reimbursement-Log.pdf"); // Change attachement by inline
readfile("Mileage-Reimbursement-Log.pdf");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    相关资源
    最近更新 更多