#user input
5 messages · Page 1 of 1 (latest)
Hi @spark sigil. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In Astro, you can create a form to get input from the user. Here's a step-by-step guide based on the information provided in the extracts:
-
Create or identify a
.astropage which will contain your form and your handling code. For example, you could add a registration page:src/pages/register.astro(source (https://docs.astro.build/en/recipes/build-forms#recipe)). -
Add a
<form>tag with some inputs to the page. Each input should have anameattribute that describes the value of that input. Be sure to include a<button>or<input type="submit">element to submit the form (source (https://docs.astro.build/en/recipes/build-forms#recipe)).
Here's an example of how your form could look:
<h1>Register</h1>
<form>
<label> Username: <input type="text" name="username" required /> </label>
<label> Email: <input type="email" name="email" required /> </label>
<label> Password: <input type="password" name="password" required minlength="6" /> </label>
<button>Submit</button>
</form>
-
Use validation attributes to provide basic client-side validation that works even if JavaScript is disabled. For example,
requiredprevents form submission until the field is filled,minlengthsets a minimum required length for the input text, andtype="email"introduces validation that will only accept a valid email format (source (https://docs.astro.build/en/recipes/build-forms#recipe)). -
The form submission will cause the browser to request the page again. Change the form’s data transfer
methodtoPOSTto send the form data as part of theRequestbody, rather than as URL parameters (source (https://docs.astro.build/en/recipes/build-forms#recipe)).
- Check for the
POSTmethod in the frontmatter and access the form data usingAstro.request.formData(). Wrap this in atry ... catchblock to handle cases when thePOSTrequest wasn’t sent by a form and theformDatais invalid (source (https://docs.astro.build/en/recipes/build-forms#recipe)).
Here's an example of how you could handle form data:
---
if (Astro.request.method === "POST") {
try {
const data = await Astro.request.formData();
const name = data.get("username");
const email = data.get("email");
const password = data.get("password");
// Do something with the data
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}
}
---
- Validate the form data on the server. This should include the same validation done on the client to prevent malicious submissions to your endpoint and to support the rare legacy browser that doesn’t have form validation (source (https://docs.astro.build/en/recipes/build-forms#recipe)).
Remember, you can also add custom validation logic that refers to multiple fields using a <script> tag and the Constraint Validation API (source (https://docs.astro.build/en/recipes/build-forms#recipe)).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@spark sigil kapa.ai is still learning and improving, please let us know how it did by reacting below