Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  145] [ 6]  / answers: 1 / hits: 11127  / 10 Years ago, sat, november 1, 2014, 12:00:00

I have the following code.



     <div onClick=registerationform.pdf>something</div>
<?php
header('Content-disposition: attachment; filename=registerationform.pdf');
header('Content-type: application/pdf');
readfile('registerationform.pdf');


This code directly downloads the output if the page is loaded. But I need the pdf to get downloaded only if the something button is clicked.Help me


More From » php

 Answers
6

Php code is executed before any page content is shown or any javascript is executed, and not exactly sequentially as you see it in your example.



What you want is probably to create another php page downloadpdf.php which includes those headers you specified, and redirect the user to that page through a link:



link.php:



<a href=downloadpdf.php target=_blank> Download PDF </a>


Note: target=_blank is added here so the actual page is not redirected but instead the new page is opened in a new tab-> the browser downloads the file and immediately closes the tab, feeling like it's an immediate download from the current page you are on.



downloadpdf.php



 <?php
header('Content-disposition: attachment; filename=registerationform.pdf');
header('Content-type: application/pdf');
readfile('registerationform.pdf');

[#41543] Thursday, October 30, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
;