# Server Actions (Forms)

> Native HTML form submissions with Progressive Enhancement.

Subject: Next.js | Lesson 31 of 65 | 7 min read
Interactive lesson: https://codewithanimation.in/nextjs/server-actions-forms
Previous: [React cache()](https://codewithanimation.in/nextjs/react-cache.md) | Next: [Server Actions (Non-Forms)](https://codewithanimation.in/nextjs/server-actions-non-forms.md)

## The End of the API Route

Server Actions allow you to run asynchronous code directly on the server without manually writing API endpoints.

In Next.js, Server Actions are deeply integrated into the native HTML `<form>` element. By passing a Server Action to the `action` attribute, Next.js wires up the client-to-server communication automatically.

## Progressive Enhancement

Because Server Actions use the native `action` attribute instead of React's `onSubmit` event handler, they support Progressive Enhancement.

This means your form will successfully submit data to your database even if the user's browser hasn't finished downloading the JavaScript bundle, or if JavaScript is completely disabled.

## The 'use server' Directive

To create a Server Action, you simply define an async function and place the `'use server'` directive at the top of its body.

Then, pass that function to the form: `<form action={createPost}>`. Next.js extracts the form fields into a standard Web `FormData` object, which your action receives as its first argument.

## Common mistake: The onSubmit Trap

React developers are accustomed to writing `<form onSubmit={handleSubmit}>`, calling `e.preventDefault()`, and manually using `fetch()` to hit a REST API.

If you do this in Next.js, your form is entirely dependent on client-side JavaScript. If the network drops while downloading your JS bundle, the user's submit button will do absolutely nothing.

## Tip: Under the Hood

If JavaScript IS enabled, Next.js hijacks the form submission and performs an optimized `fetch` request, preserving the client-side state and avoiding a full page reload.

If JavaScript is NOT enabled, it falls back to a standard HTTP POST request (a full page navigation).

## Try it: The JS Toggle

Experience Progressive Enhancement in action. Submit the form with JavaScript enabled to see Next.js perform a seamless background fetch. Then, toggle JavaScript OFF and submit again. Notice how it falls back to a native browser POST request with a full page navigation, yet still succeeds!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/server-actions-forms

## Check yourself

### Question 1

What is the primary benefit of using `<form action={myAction}>` instead of `<form onSubmit={myHandler}>`?

- A. It prevents default browser behaviors
- B. It allows the form to work even if JavaScript is disabled or fails to load
- C. It automatically validates the form data against the database schema
- D. It encrypts the form data in transit

Correct answer: B

Using the native `action` attribute provides Progressive Enhancement. The form can execute a standard HTTP POST request if the JavaScript bundle hasn't loaded.

- Why not A: onSubmit is used to prevent default behavior. action={} embraces default behavior as a fallback.
- Why not C: You must write your own validation logic (e.g., using Zod).
- Why not D: HTTPS handles encryption, not the form attributes.

### Question 2

When a Server Action is triggered by a form `action`, what is passed as the first argument to the action function?

- A. The React SyntheticEvent
- B. A JSON object containing the input values
- C. A standard Web FormData object
- D. The HTMLFormElement reference

Correct answer: C

The function receives a standard Web `FormData` object. You can extract values using `formData.get('inputName')`.

- Why not A: Events are passed to onSubmit, not action.
- Why not B: Next.js uses standard web APIs, not automatic JSON parsing.
- Why not D: It passes the extracted data, not the DOM node.

## Remember this

- Server Actions eliminate the need to write manual API routes for mutations.
- Place the `'use server'` directive inside async functions to mark them as Server Actions.
- Using `<form action={myAction}>` enables Progressive Enhancement.
- If JavaScript fails to load, Next.js falls back to a native HTTP POST request.
- Server Actions receive a `FormData` object containing the input values.
