Skip to main content
A +page.server.js file can export actions, which allow you to POST data to the server using the <form> element. When using <form>, client-side JavaScript is optional, but you can easily progressively enhance your form interactions with JavaScript to provide the best user experience.

Default actions

In the simplest case, a page declares a default action:
Actions always use POST requests, since GET requests should never have side-effects.

Invoking from other pages

You can invoke the action from other pages by adding the action attribute:

Named actions

Instead of one default action, a page can have multiple named actions:
To invoke a named action, add a query parameter with the name prefixed by a / character:

Using formaction

You can use the formaction attribute on a button to POST to a different action:

Anatomy of an action

Each action receives a RequestEvent object, allowing you to read the data with request.formData():
The returned data is available through the form property:

Validation errors

If the request couldn’t be processed because of invalid data, return validation errors using the fail function:
Display validation errors in your component:
As a precaution, we only return the email back to the page — not the password.

Redirects

Redirects work exactly the same as in load functions:

Loading data

After an action runs, the page will be re-rendered (unless a redirect or an unexpected error occurs):
1

Action completes

The action’s return value is available as the form prop
2

Load functions rerun

Your page’s load functions will run after the action completes
3

Page updates

The page is re-rendered with the new data
Note that handle runs before the action is invoked, and does not rerun before the load functions. If you use handle to populate event.locals, you must update it in the action.

Progressive enhancement

The forms we’ve built work without client-side JavaScript. When JavaScript is available, we can progressively enhance the user experience.

use:enhance

The easiest way to progressively enhance a form is to add the use:enhance action:
use:enhance can only be used with forms that have method="POST" and point to actions defined in a +page.server.js file.
Without an argument, use:enhance will emulate the browser-native behavior, but without full-page reloads:
Updates the form property and page.form on a successful or invalid response (if the action is on the same page)
Resets the <form> element after successful submission
Invalidates all data using invalidateAll on a successful response
Calls goto on a redirect response
Renders the nearest +error boundary if an error occurs
Resets focus to the appropriate element

Customizing use:enhance

To customize the behavior, provide a SubmitFunction:

Using applyAction

To get the default behavior back after customization, use applyAction:
The behavior of applyAction(result) depends on result.type:
Sets page.status to result.status and updates form and page.form to result.data

Custom event listener

You can also implement progressive enhancement with a normal event listener:
You need to deserialize the response before processing it. JSON.parse() isn’t enough because form actions support returning Date or BigInt objects.

Alternatives

Form actions are the preferred way to send data to the server, but you can also use +server.js files to expose a JSON API:

GET vs POST

As we’ve seen, to invoke a form action you must use method="POST". Some forms don’t need to POST data to the server — search inputs, for example. For these you can use method="GET":
Submitting this form will navigate to /search?q=... and invoke your load function:
As with <a> elements, you can set the data-sveltekit-reload, data-sveltekit-replacestate, data-sveltekit-keepfocus and data-sveltekit-noscroll attributes on the <form> to control the router’s behavior.

Next steps

Page options

Configure SSR, CSR, and prerendering options

State management

Manage state across server and client