re-org Form docs (#1104)
- added `/concepts/forms` - added 303 redirect in `POST` form submit code example previous PR: https://github.com/denoland/fresh/pull/1040 --------- Co-authored-by: Matthew Morek <matthew.morek@gmail.com> Co-authored-by: Luca Casonato <lucacasonato@yahoo.com> Co-authored-by: Séverin Dupouy <32488190+severindupouy@users.noreply.github.com> Co-authored-by: hashrock <hashrock@users.noreply.github.com>
This commit is contained in:
co-authored by
Matthew Morek
Luca Casonato
Séverin Dupouy
hashrock
parent
3c0c576a54
commit
ff9bb0f132
@@ -0,0 +1,59 @@
|
||||
---
|
||||
description: |
|
||||
Robustly handle user inputs using HTML `<form>` elements client side, and form
|
||||
submission handlers server side.
|
||||
---
|
||||
|
||||
For stronger resiliency and user experience, Fresh relies on native browser
|
||||
support for form submissions with the HTML `<form>` element.
|
||||
|
||||
In the browser, a `<form>` submit will send an HTML action (usually `GET` or
|
||||
`POST`) to the server, which responds with a new page to render.
|
||||
|
||||
## POST request with `multipart/form-data`
|
||||
|
||||
Forms can either submit as a `GET` request with URL search parameter encoding,
|
||||
or as a `POST` request with `multipart/form-data`.
|
||||
|
||||
This example demonstrates how to handle `multipart/form-data` `<form>`
|
||||
submissions:
|
||||
|
||||
```tsx
|
||||
// routes/subscribe.tsx
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
async GET(req, ctx) {
|
||||
return await ctx.render();
|
||||
},
|
||||
async POST(req, ctx) {
|
||||
const form = await req.formData();
|
||||
const email = form.get("email")?.toString();
|
||||
|
||||
// Add email to list.
|
||||
|
||||
// Redirect user to thank you page.
|
||||
const headers = new Headers();
|
||||
headers.set("location", "/thanks-for-subscribing");
|
||||
return new Response(null, {
|
||||
status: 303, // See Other
|
||||
headers,
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export default function Subscribe() {
|
||||
return (
|
||||
<>
|
||||
<form method="post">
|
||||
<input type="email" name="email" value="" />
|
||||
<button type="submit">Subscribe</button>
|
||||
</form>
|
||||
</>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
When the user submits the form, Deno will access a specific `email` value from a
|
||||
`formData()`, add the email to a list, and redirect the user to a thank you
|
||||
page.
|
||||
@@ -18,10 +18,10 @@ JavaScript to make them more interactive.
|
||||
|
||||
The way forms work in the browser, is that they perform an HTML navigation
|
||||
action when the user submits the form. In most cases this means that when the
|
||||
form is submitted, a GET or POST request is sent to the server with the form
|
||||
form is submitted, a `GET` or `POST` request is sent to the server with the form
|
||||
data, which then responds with a new page to render.
|
||||
|
||||
Fresh can handle both GET and POST requests through the
|
||||
Fresh can handle both `GET` and `POST` requests through the
|
||||
[custom handlers][custom-handlers] feature of routes. The handlers can perform
|
||||
any necessary processing on the form data, and then pass data to the
|
||||
`ctx.render()` call to render a new page.
|
||||
@@ -71,6 +71,9 @@ query set as the `q` query parameter in the URL. The `GET` handler will then
|
||||
filter the names array based on the query, and pass it to the page component for
|
||||
rendering.
|
||||
|
||||
[Learn more about using forms in Fresh][concepts-forms].
|
||||
|
||||
<!-- TODO(lucacasonato): link to todo app example when that is built again -->
|
||||
|
||||
[custom-handlers]: /docs/getting-started/custom-handlers
|
||||
[concepts-forms]: /docs/concepts/forms
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"pages": [
|
||||
["architecture", "Architecture"],
|
||||
["routes", "Routes"],
|
||||
["forms", "Forms"],
|
||||
["islands", "Interactive islands"],
|
||||
["static-files", "Static files"],
|
||||
["middleware", "Route middleware"],
|
||||
|
||||
Reference in New Issue
Block a user