How can PHP developers handle long processing times for form submissions without causing users to refresh the page multiple times?

When handling long processing times for form submissions in PHP, developers can use AJAX to send the form data asynchronously to the server and display a loading spinner to indicate that the form is being processed. This way, users can continue interacting with the page without having to refresh it multiple times.

// HTML form with AJAX submission
<form id="myForm">
  <!-- form fields go here -->
  <button type="submit">Submit</button>
</form>

<script>
document.getElementById('myForm').addEventListener('submit', function(e) {
  e.preventDefault();
  
  // Display loading spinner
  // Code to show loading spinner
  
  // Send form data asynchronously using AJAX
  var formData = new FormData(this);
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'process_form.php', true);
  xhr.onload = function() {
    // Handle response from server
    // Code to hide loading spinner
  };
  xhr.send(formData);
});
</script>