How can PHP scripts be executed without reloading the entire page when exporting data to Excel?

To execute PHP scripts without reloading the entire page when exporting data to Excel, you can use AJAX to send a request to a PHP script that generates the Excel file. This way, the page remains static while the PHP script runs in the background to create the file.

// AJAX request to export data to Excel without page reload
if(isset($_POST['export_excel'])){
    // Code to generate Excel file
    // This could include querying a database, formatting data, and creating the Excel file
    // Once the file is created, send it back to the client for download
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="exported_data.xlsx"');
    // Output the Excel file content here
    exit();
}