Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Simle HTML Form & Handler

This val demonstrates how to render an HTML form and handle submissions.

View Live Form | View Data

Screenshot 2023-08-28 at 15.37.04@2x.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { thisWebURL } from "https://esm.town/v/stevekrouse/thisWebURL";
import { html } from "https://esm.town/v/stevekrouse/html";
import { email as email2 } from "https://esm.town/v/std/email?v=9";
let { formDataEx } = await import("https://esm.town/v/stevekrouse/formDataEx");
export let form = async (req: Request) => {
if (req.method === "POST") { // treating POSTs as form submissions
let email = (await req.formData()).get("email") as string; // get the email address
formDataEx = formDataEx ?? []; // initialize the data val
formDataEx.push(email); // add the new form submission
await email2({ text: email, subject: "New Submission!" }); // email yourself about it
return html(`Thanks ${email}!`); // thank the submitter
}
return html(`
<form action="${thisWebURL()}" method="post">
Email <input name="email" /> <button>Submit</button>
</form>`); // render an HTML form
};
stevekrouse-form.web.val.run
October 23, 2023