-2

I need to download PDF files simultaneously by selecting textboxes and clicking on a Download button. For now, I am able to click the Download button next to each item to download the item. I need to have another button which will be used for bulk download of the files.

Below is the code for the Download button next to each item.

<td><a href="print_voucher.php?em_id=<?php echo $row['voucher_id'] ?>" download="print_voucher.php?em_id=<?php echo $row['voucher_id'] ?>"> Download </a></td>

Below is the code for the whole table displaying the items to download.

<form action="createzip.php" method="post">
    <table id="selection_table" class="data-table table stripe hover nowrap">
        <thead>
            <tr>
                <div class="col-md-6">
                </div>
                <div class="col-md-1">
                </div>
                <div class="col-md-2">
                </div>
                <div class="col-md-3 pd-10 text-center">
                    <a class="btn btn-primary" name="createzip" id="createzip" value="createzip"> DOWNLOAD ALL </a>
                </div>
            </tr>
            <tr>
                <th class="table-plus datatable-nosort"> <input id="select_all" style="max-height: 20px; max-width: 20px;" type="checkbox" class="form-control" value="Select All"/> <p> Select All </p></th>
                <th> COMPANY NAME </th>
                <th> EMPLOYEE NAME </th>
                <th> DOWNLOAD LINK </th>
            </tr>
        </thead>
        <tbody>
            <tr>

                <?php
                
                    $sql = "SELECT * FROM vouchers WHERE voucher_id BETWEEN '$empID' AND '$endEmpID'";
                    $query = mysqli_query($conn, $sql) or die(mysqli_error());
                    while ($row = mysqli_fetch_array($query)) {
                    $id = $row['voucher_id'];
                 ?>  

                <td class="table-plus">
                    <input style="max-height: 20px; max-width: 20px;" name="fieldid[]" id="checkbox<?php echo $row['voucher_id'] ?>" value="<?php $row['voucher_id'] ?>" type="checkbox" class="form-control">
                </td>
                <td><?php echo $row['paid_by']; ?></td>
                <td><?php echo $row['paid_by']; ?></td>
                <td><a href="print_voucher.php?em_id=<?php echo $row['voucher_id'] ?>" download="print_voucher.php?em_id=<?php echo $row['voucher_id'] ?>"> Download </a></td>

            </tr>
            <?php }?>
        </tbody>
    </table>
</form>

How to download the files simultaneously?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • https://stackoverflow.com/questions/18451856/how-can-i-let-a-user-download-multiple-files-when-a-button-is-clicked – dmoo Nov 03 '22 at 09:47
  • Does this answer your question? [Download multiple files with a single action](https://stackoverflow.com/questions/2339440/download-multiple-files-with-a-single-action) – Pouya Esmaeili Nov 05 '22 at 21:36

1 Answers1

-2

You can do it with jquery/js when click button. Firstly, you need to add download button to the html tags

<button class='download'> Download files </button>

After it, add the below codes to the script tag at end of the file :

$('.download_button').click(function(e) {
    e.preventDefault();

    $('.table-plus input:checked').each(function (e){
        window.open('http://yoursite.com/print_voucher.php?em_id='+this.value);
    });
});

Additionally, if you want to download any files also PDF, you will need to change server headers for download files like that (it is server side - PHP read example):

<?php
header("Content-type:application/pdf");

// It will be called downloaded.pdf
header("Content-Disposition:attachment;filename='myFile.pdf'");

readfile("myFile.pdf");
Tural Rzaxanov
  • 783
  • 1
  • 7
  • 16
  • Thank you so much. I tried it out. It does view the PDF files on the browser, but it does not download it into my computer as the download button does. The download button has the href="" and download="" attributes which makes the files to be downloaded into my computer. I am not so sure if is there a way which I can try implement those attributes to just directly download the files straight into the computer. – Bonginkosi Nov 04 '22 at 07:26
  • Here you are. Bro to download, I think you will need to set some headers from server. I will change my answer for this mode. – Tural Rzaxanov Nov 04 '22 at 08:00