Submit Laravel form using JavaScript

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Submit Laravel Forms using JavaScript: Achieving Seamless AJAX Submissions

As developers working with modern frameworks like Laravel and Blade, we often encounter a common challenge: how to handle form submissions without forcing a full page reload. The default behavior of an HTML <form> tag is to navigate to a new URL upon submission, which breaks the smooth, interactive experience that modern Single Page Applications (SPAs) aim for.

If you are using Laravel, your backend is designed to handle data via API endpoints. The solution lies in decoupling the form submission from the browser's default behavior and sending the request asynchronously using JavaScript, typically through the fetch API.

This post will walk you through exactly how to achieve this, focusing on correctly structuring your request body when interacting with a Laravel backend.

Why Avoid Page Reloads? The Power of AJAX

When a form submits traditionally, the browser performs a full page refresh. This is disruptive. By using JavaScript's fetch or XMLHttpRequest, we can intercept the submission event, prevent the default action, collect the form data, and send it to the server in the background. The result (success message, error, redirection) can then be handled dynamically on the client side without losing context. This results in a much smoother user experience, which is a core principle of modern web development, aligning perfectly with the principles taught by platforms like Laravel Company.

Mastering the fetch Request for Laravel

Your initial attempt using fetch was on the right track, but the key missing piece was correctly formatting the data you are sending in the request body. When interacting with a RESTful API built by Laravel, the server expects data in a specific format—usually JSON.

To send data via POST, PUT, or PATCH requests using fetch, you need to configure three main parts: the URL, the method, and the request body.

Step 1: Capturing Form Data

First, we attach an event listener to the form's submit event to call e.preventDefault(). This stops the browser from executing its default navigation. Next, we collect the data from the input fields.

Step 2: Constructing the Request Body

For sending structured data to a Laravel controller, you must serialize your data into a JSON string and set the appropriate Content-Type header. This tells the server how to interpret the incoming stream of data.

Here is a comprehensive example demonstrating how to submit form data asynchronously.

document.querySelector('#update-form').addEventListener('submit', async (e) => {
    e.preventDefault(); // Stop the default form submission

    const form = e.target;
    const formData = new FormData(form); // Collect all form fields as FormData object

    // Prepare data for fetch: Convert FormData to a standard object for easier handling, 
    // or use JSON if you are sending structured data explicitly.
    const dataToSend = Object.fromEntries(formData.entries());

    try {
        const response = await fetch(form.action, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json', // Crucial for telling Laravel we are sending JSON
                // If you were using Laravel's CSRF protection via session, 
                // you might need to pass a token header here as well.
            },
            body: JSON.stringify(dataToSend) // Convert the JavaScript object into a JSON string
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const result = await response.json();
        console.log('Success:', result);
        // Handle success state on the client side (e.g., display a success message)

    } catch (error) {
        console.error('Submission failed:', error);
        // Handle errors gracefully
    }
});

Backend Consideration in Laravel

When your JavaScript sends data as application/json, your Laravel controller must be configured to receive it correctly. This is typically done by ensuring your route handles the POST request and expects the input via the request body, often using methods like request()->json('field_name'). Furthermore, always ensure you are using Laravel's built-in security features, such as the @csrf directive in your Blade templates, to protect against Cross-Site Request Forgery.

Conclusion

Submitting forms with JavaScript is a fundamental skill for building dynamic, modern web applications. By understanding how to use e.preventDefault(), structuring data correctly into JSON objects, and setting the appropriate Content-Type header in your fetch request, you can bypass traditional page reloads and create a highly responsive user interface. Mastering this technique allows you to leverage the power of AJAX while maintaining the robust structure provided by frameworks like Laravel.