1

i created a php page Download a PDF file only if a checkbox is checked. i open the pdf file when checkbox is click by the user but i need give direct download the PDF file when user click. pls help it

 <script>
 function toggleCheckbox(element)
 {

   window.open('pdf/Code_of_Med_Ethics-a.pdf') ;
 }
 </script>
<input type="checkbox" onchange="toggleCheckbox(this.id)" name="click" value="agree" id="click"} ?> >

2 Answers2

0
if($("#click").is(':checked')){
    e.preventDefault();  //stop the browser from following
    window.location.href = 'pdf/Code_of_Med_Ethics-a.pdf';
}
M Reza Saberi
  • 7,134
  • 9
  • 47
  • 76
  • i want download the fileon checked not open the pdf. this open the file. –  Jan 13 '15 at 05:36
  • @AmitKumar - that's because you're sending the wrong headers, and that can't be fixed with javascript, you have to send the right headers to start a download in PHP. – adeneo Jan 13 '15 at 05:48
  • http://stackoverflow.com/questions/20080341/correct-php-headers-for-pdf-file-download – adeneo Jan 13 '15 at 05:49
0

try this code

<input type="checkbox" onClick="toggleCheckbox(this.id)" name="click" value="agree" id="click" > 
<script>
 function toggleCheckbox(element)
 {
    if(document.getElementById(element).checked) {
       window.open('pdf/Code_of_Med_Ethics-a.pdf') ;
    }   
}
</script>
Nishit Maheta
  • 6,021
  • 3
  • 17
  • 32