I am pretty new to PHP but I am learning fast.. I have been trying to use AJAX to Pass An ID to PHP. With this ID I use SQL to get the path of a file from the database than use it to download the file. However after some research this is not possible and I couldn't understand alternives... maybe there is a workaround?
These are my JavaScript function; I used JSON because I was passing an Array of IDs because I intend to use it in future for multiple downloads. I get the IDs from a table row click.
$('.btnDownload').click(function() {
$.ajax({
url: 'scripts/downloadFile.php',
type: "POST",
data: {id: JSON.stringify(fileID)}
});
});
And my PHP to attempt to download the file;
<?php
$data = $_POST['id'];
$data = json_decode("$data", true);
$countArray = count($data);
$counter = 0;
while($countArray > $counter){
$getID = $data[$counter];
// My sql connections and queries, jumped to the fetch part
$filePath = $fetch['filePath'];
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($filePath));
readfile($filePath);
exit;
$counter++;
}
?>
How ever this script alone works when I give a static path and run it with my browser. I would like some help on alternatives on achieving the results.