How can the lack of page reload after clicking a download button in PHP be addressed effectively?

Issue: The lack of page reload after clicking a download button in PHP can be addressed effectively by using AJAX to handle the download request asynchronously without refreshing the page.

// PHP code snippet using AJAX to handle download button click without page reload
<button id="downloadButton">Download File</button>
<script>
document.getElementById('downloadButton').addEventListener('click', function() {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'download.php', true);
    xhr.responseType = 'blob';
    xhr.onload = function() {
        if (this.status === 200) {
            var blob = new Blob([this.response], { type: 'application/octet-stream' });
            var url = window.URL.createObjectURL(blob);
            var a = document.createElement('a');
            a.href = url;
            a.download = 'file.txt';
            document.body.appendChild(a);
            a.click();
            window.URL.revokeObjectURL(url);
        }
    };
    xhr.send();
});
</script>