Created
September 3, 2022 14:30
-
-
Save skolhustick/b306b20ca5d248d44dece08fee8fcaa9 to your computer and use it in GitHub Desktop.
astro-mongodob-CreateUserForm.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/components/CreateUserForm.svelte | |
<script> | |
const onSubmit = async (e) => { | |
const formData = new FormData(e.target); | |
const data = {}; | |
for (let field of formData) { | |
const [key, value] = field; | |
data[key] = value; | |
} | |
try { | |
await fetch("/api/users", { | |
method: "POST", | |
body: JSON.stringify(data), | |
}); | |
window.location.replace("/users"); | |
} catch (error) { | |
alert("Oops. We failed."); | |
} | |
}; | |
</script> | |
<form on:submit|preventDefault={onSubmit}> | |
<input type="text" name="name" required /> | |
<input type="email" name="email" required /> | |
<input type="integer" name="age" required /> | |
<button type="submit">Submit</button> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment