Austin
Austin

Reputation: 1627

using php to show a pdf file?

I am trying to use php to check form variables and if everything checks out then present the user with a pdf document stored on my server in the same directory as my php file. The code I'm using kind of works meaning it will download the pdf on the users computer but does not automatically display it. I would like to make it so if my validation passes the user is redirected to the pdf document. If my validation fails then the user is presented with an error message.

I even tried using jQuery to post the form data in hopes that it would return the pdf to the user but that does not work.

Does anyone know how to achieve what I am looking for?

My 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>

My 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";    
}
?>

My jQuery that I tried:

$('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;
    }
});

Upvotes: 0

Views: 223

Answers (1)

Isaac
Isaac

Reputation: 983

Don't need the JQuery code. Replace attachement by inline

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

Upvotes: 1

Related Questions