Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnw65's avatar

Processing Animation HTTP Request - not using ajax

I'm running a FPDF application via HTTP Request. However, to generate the report takes a long time. What is the best method to display an animation while the report is being downloaded. Thanks.

0 likes
1 reply
LaryAI's avatar
Level 58

One possible solution to display an animation while the report is being downloaded is to use JavaScript and AJAX to handle the HTTP request and update the UI accordingly.

Here's an example of how you can achieve this using jQuery:

  1. Create an HTML element to display the animation, such as a loading spinner or a progress bar. Give it an ID for easier manipulation.
<div id="loading-spinner"></div>
  1. Include the jQuery library in your HTML file.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  1. Use JavaScript to make an AJAX request to fetch the report data. Show the loading animation before the request is sent and hide it after the response is received.
$(document).ready(function() {
    // Show the loading animation
    $('#loading-spinner').show();

    // Make the AJAX request
    $.ajax({
        url: 'path/to/report',
        method: 'GET',
        success: function(response) {
            // Hide the loading animation
            $('#loading-spinner').hide();

            // Process the response and update the UI
            // For example, you can display the report data in a div
            $('#report-container').html(response);
        },
        error: function() {
            // Handle any errors that occur during the request
            // For example, you can display an error message
            $('#loading-spinner').hide();
            $('#error-message').text('An error occurred while fetching the report.');
        }
    });
});
  1. Style the loading animation using CSS to make it visually appealing.
#loading-spinner {
    /* Add your styles here */
}

Remember to replace 'path/to/report' with the actual URL of your report generation endpoint.

This solution uses jQuery's AJAX function to send an HTTP GET request to fetch the report data. While the request is being processed, the loading animation is displayed. Once the response is received, the animation is hidden, and the report data is displayed in the designated container element.

Note: This solution assumes you have a server-side endpoint that generates the report and returns the data. You'll need to implement that part separately.

Please or to participate in this conversation.