Skip to content
PHP Logo

What are the potential pitfalls of keeping a user on a waiting page in PHP?

Potential pitfalls of keeping a user on a waiting page in PHP include increased server load, decreased user experience, and potential timeouts. To solve this issue, consider implementing asynchronous processing or utilizing AJAX to update the page without requiring the user to wait.

php
// Example of using AJAX to update the page without keeping the user on a waiting page

// HTML code to display a loading spinner
echo '<div id="loading-spinner" style="display: none;">Loading...</div>';

// JavaScript code to send an AJAX request and update the page
echo '<script>
    // Show loading spinner
    document.getElementById("loading-spinner").style.display = "block";
    
    // Send AJAX request to process data
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "process_data.php", true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            // Update page content with processed data
            document.getElementById("content").innerHTML = xhr.responseText;
            // Hide loading spinner
            document.getElementById("loading-spinner").style.display = "none";
        }
    };
    xhr.send();
</script>';