In this episode, we dive into how Inertia handles form submission and shows off its ability to update the page without a full reload. First up, we take our comment form and hook it up so it actually submits data using an event handler and a method named createPost
. Instead of a normal browser form submission (which would reload the page), we prevent default behavior and handle everything with JavaScript and Inertia.
We then set up a new controller in Laravel (the CommentStoreController) and add the appropriate route to handle storing comments. Once that's ready, we use Inertia to send the form data—just like a regular API request—using the form.post()
method. After the form is submitted, Inertia intercepts the redirect and instead of reloading the entire page, it re-fetches the data just for the current component. This means you see your new comment show up right away, and the page never actually refreshes.
We check out what's happening in the Network tab, walking through the behind-the-scenes requests: first the submission, then the data re-fetch that provides the updated props including the new comment. All of this happens smoothly without any full-page reloads, making for a much snappier user experience.
Finally, we hint at future tweaks—like clearing the form after submission—to make our form handling even better. If you've ever wondered how to get that Single Page Application feel while still using Laravel and server-side routing, this episode shows exactly how Inertia makes it work.