How do I post a form in laravel 5 using ajax?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Mastering AJAX Form Submissions in Laravel: A Deep Dive for Beginners As a senior developer, I’ve seen countless developers struggle with the synchronization between frontend form submissions and backend processing, especially when introducing Asynchronous JavaScript and XML (AJAX). The confusion often stems from uncertainty about what data to send and, critically, how to handle validation errors gracefully. If you are working in a Laravel environment—even an older version like Laravel 5—understanding this flow is essential for building robust applications. This guide will walk you through the complete process of posting a form via AJAX in Laravel, focusing on best practices, utilizing Form Requests for validation, and correctly handling the JSON responses that drive modern web interactivity. ## The Challenge: Bridging Frontend and Backend with AJAX The core difficulty when using AJAX is knowing how to structure the request so that the server can respond predictably. When a form is submitted via standard HTML, the browser handles the submission directly. With AJAX, we take over that process in JavaScript, which means we need to manually manage headers, data serialization, and most importantly, error reporting. Your provided setup—using jQuery's `$.ajax` with `$(this).serialize()`—is a perfectly valid starting point for sending form data. However, the real complexity lies in what happens on the server side when validation fails. ## Step 1: Enforcing Validation with Form Requests The first step in any secure Laravel application is to delegate validation logic away from your controller and into dedicated **Form Requests**. This aligns perfectly with the principles of clean, maintainable code promoted by teams at [laravelcompany.com](https://laravelcompany.com). In your example, you correctly defined `CreateRegisterRequest`. This class handles all the rules (e.g., `'required'`, `'unique:users,email'`). When a request fails validation within a Form Request, Laravel automatically stops execution and prepares an error response. **Best Practice:** Do not manually check for errors in your controller. Let the framework handle it. ## Step 2: Modifying the Controller to Return JSON Responses Since you are using AJAX, the server should *not* return a full HTML view on failure. Instead, it must return structured data—usually JSON—so the JavaScript can parse it and display specific error messages to the user. If your `CreateRegisterRequest` fails validation, Laravel’s built-in mechanism will throw an exception. To catch this and return a helpful response, you need to handle the successful case (a redirect or successful creation) and the failed case (returning errors). Here is how you adjust your controller method: ```php json(['success' => true, 'message' => 'Registration successful!']); } // ... other methods } ``` ### Handling Validation Errors in JSON If validation fails inside the `CreateRegisterRequest`, Laravel will automatically throw an exception. To catch this and return meaningful error messages to your AJAX request, you need to implement an exception handler or leverage the response mechanism. A cleaner approach for handling Form Request failures is often using the `validate()` method directly on the request object if you are not using a dedicated HTTP layer for exceptions: ```php public function create(CreateRegisterRequest $request) { // The request object already has validation errors attached if validation fails. if ($request->fails()) { // Return 422 Unprocessable Entity status code with error details return response()->json([ 'success' => false, 'errors' => $request->errors() // Access the detailed errors provided by Laravel ], 422); } // If validation passed, proceed with creation... return response()->json(['success' => true, 'message' => 'Registration successful!']); } ``` *Note: While using Form Requests is highly recommended for validation (as shown in your example), manually checking `$request->fails()` or catching HTTP exceptions allows you to dictate the exact JSON payload sent back over AJAX.* ## Step 3: Executing the AJAX Request Your JavaScript code, which initiates the request, must be prepared to handle both success and error responses. Notice how we explicitly set `dataType: 'json'` and define separate `success` and `error` callbacks. ```javascript $(function(){ $('#sign-up').on('submit', function(e){ $.ajax({ type: "POST", url: '/register', data: $(this).serialize(), // Sends form data as key=value pairs dataType: 'json', // Expecting a JSON response from the server success: function(data){ if (data.success) { console.log('Success:', data.message); alert(data.message); // Optionally clear the form here } else { // Handle application-level errors returned by Laravel console.error('Validation Failed:', data.errors); // Display specific errors next to the form fields using jQuery/DOM manipulation } }, error: function(jqXHR, textStatus, errorThrown){ // Handle HTTP errors (e.g., 422 Unprocessable Entity) from Laravel console.error('AJAX Error:', textStatus, errorThrown); if (jqXHR.status === 422) { // Display the specific form validation errors returned by the server let errors = jqXHR.responseJSON.errors; console.log('Server Errors:', errors); // Logic to inject these errors into the corresponding HTML fields goes here. } else { alert('An unknown error occurred.'); } } }); e.preventDefault(e); }); }); ``` ## Conclusion Posting a form via AJAX in Laravel is less about knowing a single magic command and more about establishing a clear contract between your frontend and backend. By leveraging **Form Requests** for strict validation, returning structured **JSON responses** from your controller (including explicit error messages), and using robust **AJAX callbacks**, you create an application that is not only functional but also reliable and developer-friendly. Stick to these patterns, and you will build scalable applications that feel intuitive, just like those developed within the Laravel ecosystem.