+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 adefault 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 theaction attribute:
Named actions
Instead of onedefault action, a page can have multiple named actions:
/ character:
- Same page
- Different page
Using formaction
You can use theformaction attribute on a button to POST to a different action:
Anatomy of an action
Each action receives aRequestEvent object, allowing you to read the data with request.formData():
form property:
Validation errors
If the request couldn’t be processed because of invalid data, return validation errors using thefail function:
As a precaution, we only return the email back to the page — not the password.
Redirects
Redirects work exactly the same as inload 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 prop2
Load functions rerun
Your page’s
load functions will run after the action completes3
Page updates
The page is re-rendered with the new data
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 theuse: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.use:enhance will emulate the browser-native behavior, but without full-page reloads:
Update form property
Update form property
Updates the
form property and page.form on a successful or invalid response (if the action is on the same page)Reset form
Reset form
Resets the
<form> element after successful submissionInvalidate data
Invalidate data
Invalidates all data using
invalidateAll on a successful responseHandle redirects
Handle redirects
Calls
goto on a redirect responseShow errors
Show errors
Renders the nearest
+error boundary if an error occursReset focus
Reset focus
Resets focus to the appropriate element
Customizing use:enhance
To customize the behavior, provide aSubmitFunction:
Using applyAction
To get the default behavior back after customization, useapplyAction:
applyAction(result) depends on result.type:
- success/failure
- redirect
- error
Sets
page.status to result.status and updates form and page.form to result.dataCustom event listener
You can also implement progressive enhancement with a normal event listener: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 usemethod="POST".
Some forms don’t need to POST data to the server — search inputs, for example. For these you can use method="GET":
/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