0

I have a PHP script that allows a user to download a file. It works fine if I create a standard button on my form and, on post back, call the PHP script. However, I'm trying to have the button call some Javascript that then uses AJAX to call the PHP script. I don't get any errors but the file doesn't download.

//JAVASCRIPT
<script type="text/javascript">
function downloadForm()
{
    var data = {ipAddress : "<?php echo $ipAddress; ?>", userID : "<?php echo $userID; ?>"};
    jQuery.ajax({
        url: "/download-form-internal.php",
        type:'POST',
        data: data,
        dataType: 'html',
        success: function(result){

        },          
    });
}
</script>   

//PHP CODE SNIPPET
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename='.$FILENAME);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
echo $fileToDownload;
exit();

Thanks for any help!

Jason
  • 1,105
  • 3
  • 16
  • 30
  • is all in the same file ? –  Feb 02 '16 at 15:53
  • @DiegoMariani No - the javascript is in the main page file and the PHP code is just a snippet from the PHP file that retrieves the data and downloads the file. – Jason Feb 02 '16 at 15:55
  • With Ajax calls, `header()` function isn't going to work. Use JavaScript redirect instead. – Rehmat Feb 02 '16 at 15:57
  • 1
    Perhaps you should refer to this post http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – Tan See Youu Feb 02 '16 at 15:58

1 Answers1

0

You have success: function may be you need to add error: function to see errors.

And also why you send echo and before send header! Header is need to send first.

Michael
  • 1,089
  • 1
  • 11
  • 28