|
| 1 | +<script> |
| 2 | + import { CodeBlock } from 'svhighlight'; |
| 3 | + import { onMount } from 'svelte'; |
| 4 | +
|
| 5 | + import 'highlight.js/styles/base16/dracula.css'; |
| 6 | + import Button from '$ui/button/button.svelte'; |
| 7 | +
|
| 8 | + let insideCode = ` |
| 9 | + <form method='POST'> |
| 10 | + <div> |
| 11 | + <label for="name">Name:</label> |
| 12 | + <input type="text" id="name" name="name" required> |
| 13 | + </div> |
| 14 | + <div> |
| 15 | + <label for="email">Email:</label> |
| 16 | + <input type="email" id="email" name="email" required> |
| 17 | + </div> |
| 18 | + <button type="submit">Submit</button> |
| 19 | + </form> |
| 20 | + `; |
| 21 | + let code = |
| 22 | + ` |
| 23 | + <script lang='ts'>` + |
| 24 | + ` |
| 25 | + export let data; |
| 26 | + export let form; |
| 27 | + </` + |
| 28 | + `script> |
| 29 | + ` + |
| 30 | + insideCode; |
| 31 | +
|
| 32 | + let servercode = `import type { Actions } from "./$types"; |
| 33 | +
|
| 34 | +export const actions: Actions = { |
| 35 | + defualt: async ({ request }) => { |
| 36 | + let form = await request.formData(); |
| 37 | + console.log(form) // Form Data |
| 38 | + return { success: true }; |
| 39 | + } |
| 40 | +};`; |
| 41 | +</script> |
| 42 | + |
1 | 43 | <h1>Simple Form Submission using Svelte</h1> |
2 | 44 | <p class="mb-2"> |
3 | 45 | Form submission on the web typically involves sending data from a web form to a server for |
|
18 | 60 | data to a server via an HTTP POST request, processing the data on the server, and then responding |
19 | 61 | to the client's request with the appropriate content |
20 | 62 | </p> |
| 63 | +<div class="not-prose"> |
| 64 | + <CodeBlock |
| 65 | + language="svelte" |
| 66 | + {code} |
| 67 | + showHeader={true} |
| 68 | + showLineNumbers={false} |
| 69 | + headerText="Simple Form Submission using Svelte" |
| 70 | + /> |
| 71 | +</div> |
| 72 | +<h4>Form submission at Server Side <code>+page.server.ts</code> File</h4> |
| 73 | +<div class="not-prose"> |
| 74 | + <CodeBlock |
| 75 | + language="ts" |
| 76 | + code={servercode} |
| 77 | + showHeader={true} |
| 78 | + showLineNumbers={false} |
| 79 | + headerText="Server Side Code for Form Submission using Svelte" |
| 80 | + /> |
| 81 | +</div> |
| 82 | +<div class="mt-2"> |
| 83 | + 6. <strong>Response from the Server</strong>: After processing the form data, the server typically |
| 84 | + sends back a response to the client (browser). This response might be a new web page, a |
| 85 | + confirmation message, or any other content determined by the server-side processing. |
| 86 | + <br /> |
| 87 | + 7. <strong>Displaying the Response</strong>: The browser receives the response from the server and |
| 88 | + displays it to the user. This might involve loading a new page, updating parts of the current page |
| 89 | + dynamically (using AJAX), or displaying a pop-up message. |
| 90 | +</div> |
0 commit comments