Show Loader when submitting form In laravel collective
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Show Loader When Submitting Forms in Laravel: A Developer's Guide
As a senior developer working with the Laravel ecosystem, you frequently encounter scenarios where managing user expectations during backend operations is crucial for a good user experience. You've asked a very common question: How do I show a loading indicator (loader) when submitting a form that results in a standard server redirect, especially when not using AJAX?
While your current implementation using standard POST redirects works perfectly from a functional standpoint—the data is saved and the user is redirected correctly—it lacks the immediate visual feedback that modern web applications demand. The challenge lies in the nature of synchronous HTTP requests: once the browser issues the request, it must wait for the server to process the entire request and send the new page before it can render anything.
This post will explore why the standard method is cumbersome and, more importantly, provide the superior, modern solution using asynchronous requests (AJAX), which is the industry standard for handling dynamic form submissions with real-time loading states in Laravel.
The Synchronous Dilemma: Why Standard Redirects Fail for Loaders
In your provided example:
// Controller logic
public function patient_signup()
{
if ($result) {
return redirect(route('home'))->with('success', $message);
} else {
return Redirect::back()->withInput()->with('error', $message);
}
}
When the user clicks submit, the browser sends the request to the server. The server processes the logic (database interaction, validation), determines the redirect path, and then sends the final HTML response instructing the browser to navigate to a new URL. There is no intermediate step where the server can pause execution to send a "loading" signal back to the client during this process.
Therefore, if you are waiting for a full page reload, there is no mechanism built into standard form submissions to inject an active spinner during that wait time. This is why we move away from synchronous handling when real-time feedback is required.
The Robust Solution: Embracing Asynchronous Requests (AJAX)
The definitive solution for showing a loading state during backend processing is to leverage Asynchronous JavaScript and XML (AJAX). AJAX allows the client-side JavaScript to send an asynchronous request to the server, receive a response immediately, and update the UI without forcing a full page reload. This gives you complete control over when the loader appears and disappears.
Step 1: Modify the Controller for JSON Responses
Instead of redirecting the user immediately, your controller should return data (usually JSON) that tells the frontend whether the operation succeeded or failed, along with any necessary messages.
// Example using Laravel's response helper
public function patient_signup(Request $request)
{
// Perform validation and saving logic here...
if ($result) {
return response()->json([
'success' => true,
'message' => 'Signup successful!',
'redirect' => route('home') // Tell the frontend where to go next
], 200);
} else {
return response()->json([
'success' => false,
'error' => 'Validation failed. Please check your inputs.',
'errors' => $errors // Return validation errors if any
], 422);
}
}
Step 2: Implementing the Loader in Blade and JavaScript
On the frontend (in your Blade view), you will structure your form submission to use JavaScript's fetch API or jQuery's $.ajax() method. This is where the loader logic lives.
Blade Structure: You need a container element for your spinner.
<form id="signupForm">
<!-- Form inputs here -->
<button type="submit" id="submitBtn">Submit Registration</button>
<!-- The Loader Element -->
<div id="loader" style="display: none;">
<p>Processing request, please wait...</p>
<!-- You can add a spinner icon here -->
</div>
<!-- Area to display results/errors -->
<div id="responseMessage"></div>
</form>
JavaScript Logic (Conceptual Example): The JavaScript code will intercept the form submission, show the loader immediately, send the AJAX request, and then hide the loader based on the server's response.
document.getElementById('signupForm').addEventListener('submit', function(e) {
e.preventDefault(); // Stop the default synchronous form submission
const submitBtn = document.getElementById('submitBtn');
const loader = document.getElementById('loader');
const responseMessage = document.getElementById('responseMessage');
// 1. Show Loader and Disable Button
loader.style.display = 'block';
submitBtn.disabled = true;
responseMessage.innerHTML = '';
fetch('{{ route('patient_signup') }}', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// Include CSRF token if required by your setup
},
body: JSON.stringify(/* Form data here */)
})
.then(response => response.json())
.then(data => {
// 3. Hide Loader and Process Response
loader.style.display = 'none';
submitBtn.disabled = false;
if (data.success) {
responseMessage.innerHTML = `<p style="color: green;">${data.message}</p>`;
// Optionally redirect after success
window.location.href = data.redirect;
} else {
responseMessage.innerHTML = `<p style="color: red;">Error: ${data.error || 'An unknown error occurred.'}</p>`;
}
})
.catch(error => {
// Handle network errors
loader.style.display = 'none';
submitBtn.disabled = false;
responseMessage.innerHTML = '<p style="color: red;">Network error occurred.</p>';
console.error('Fetch error:', error);
});
});
Conclusion
While the simple POST redirect is efficient for traditional web flows, it doesn't offer the granular control needed to display a loading indicator during server processing. As a Laravel developer, mastering asynchronous patterns like AJAX is essential for building modern, responsive applications. By shifting your backend logic to return JSON responses and handling the UI state with JavaScript, you gain the ability to provide seamless, real-time feedback to your users. Always aim for this robust pattern when implementing dynamic form interactions within the Laravel framework. For more deep dives into structuring API responses in Laravel, exploring resources from laravelcompany.com is highly recommended.