|
| 1 | +# Forms with multiple steps |
| 2 | + |
| 3 | +Multi-steps forms are forms where the user has to go through multiple pages |
| 4 | +to fill in all the information. |
| 5 | +They are a good practice to improve the user experience |
| 6 | +on complex forms by removing the cognitive load of filling in a long form at once. |
| 7 | +Additionally, they allow you to validate the input at each step, |
| 8 | +and create dynamic forms, where the next step depends on the user's input. |
| 9 | + |
| 10 | +There are multiple ways to create forms with multiple steps in SQLPage, |
| 11 | +which vary in the way the state of the partially filled form |
| 12 | +is persisted between steps. |
| 13 | + |
| 14 | +This example illustrates the main ones. |
| 15 | +All the examples will implement the same simple form: |
| 16 | +a form that asks for a person's name, email, and age. |
| 17 | + |
| 18 | +## Storing the state in the database |
| 19 | + |
| 20 | +You can store the state of the partially filled form in the database, |
| 21 | +either in the final table where you want to store the data, |
| 22 | +or in a dedicated table that will be used to store only partial data, |
| 23 | +allowing you to have more relaxed column constraints in the partially filled data. |
| 24 | + |
| 25 | + - **advantages** |
| 26 | + - the website administrator can access user inputs before they submit the final form |
| 27 | + - the user can start filling the form on one device, and continue on another one. |
| 28 | + - the user can have multiple partially filled forms in flight at the same time. |
| 29 | + - **disadvantages** |
| 30 | + - the website administrator needs to manage a dedicated table for the form state |
| 31 | + - old partially filled forms may pile up in the database |
| 32 | + |
| 33 | +## Storing the state in cookies |
| 34 | + |
| 35 | +You can store each answer of the user in a cookie, |
| 36 | +using the |
| 37 | +[`cookie` component](https://sql.datapage.app/component.sql?component=cookie#component). |
| 38 | +and retrieve it on the next step using the |
| 39 | +[`sqlpage.cookie` function](https://sql.datapage.app/functions.sql?function=cookie#function). |
| 40 | + |
| 41 | + - **advantages** |
| 42 | + - simple to implement |
| 43 | + - if the user leaves the form before submitting it, and returns to it later, |
| 44 | + the state will be persisted. |
| 45 | + - works even if some of the steps do not use the form component. |
| 46 | + - **disadvantages** |
| 47 | + - the entire state is re-sent to the server on each step |
| 48 | + - the user needs to have cookies enabled to fill in the form |
| 49 | + - if the user leaves the form before submitting it, the form state will keep being sent to all the pages he visits until he submits the form. |
| 50 | + |
| 51 | +## Storing the state in hidden fields |
| 52 | + |
| 53 | +You can store the state of the partially filled form in hidden fields, |
| 54 | +using `'hidden' as type` in the [form component](https://sql.datapage.app/component.sql?component=form#component). |
| 55 | + |
| 56 | + - **advantages** |
| 57 | + - simple to implement |
| 58 | + - the form state is not sent to the server when the user navigates to other pages |
| 59 | + - **disadvantages** |
| 60 | + - the entire state is re-sent to the server on each step |
| 61 | + - you need to reference all the previous answers in each step |
| 62 | + - no *backwards navigation*: the user has to fill in the steps in order. If they go back to a previous step, you cannot prefill the form with the previous answers, or save the data they have already entered. |
0 commit comments