Mini lesson · English

How to save form submissions in a database

Save a website form in a supplied practice database project, then verify the record survives reopening the page.

Start the interactive lessonRead the lesson below

Who is this for?

People who want website submissions to be saved. A coding assistant can apply the provided setup and connect the form; writing SQL or server code is not required.

Before you start

Your own disposable Supabase project and Netlify-function practice project. Use the complete supplied files and a coding assistant for setup. Keep private database credentials on the server and test only synthetic messages. Worked answers are illustrative examples, not completed tests on your account. Replace their sample observations with your own results, or identify the step that is still blocked.

Estimated time: 20–37 min

Estimates for the supplied starter files with prerequisite accounts and tools ready. Setup, deployment queues and delivery/DNS waits take additional time. Account setup is additional. The restricted lab is not a public anonymous feedback service; review durable abuse limits before adapting it for public use.

What you will learn

  • Send a form record to real storage.
  • Verify persistence in the provider’s table view.
  • Reject invalid input without exposing everyone’s records.

Workshop files

Step 1

Define what saving means

The record must exist outside the current browser page.

Open the ready form with your assistant and choose a distinctive synthetic message. We will save it through the provided server into a separate practice database.

A value still shown on the page is not proof of database storage. Plan to find the same message in the provider and after reopening.

A concrete example

The provider table and the current browser page are different places. A message that remains in the table after closing the app demonstrates more than a sentence still displayed beside the submit button.

Use the supplied feedback-storage project. Help apply its provided schema only to my separate Supabase practice database and configure the private server environment. Keep visitor access to the feedback table restricted. Start the form, guide me to the provider table view and help check that valid data persists while invalid requests create no row. Explain my checks, not SQL syntax.

Supabase: database

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Store a record outside the browser

A saved database row should still exist after you close the page.

A JavaScript variable disappears when its page closes. Browser local storage stays on that browser. A database stores records on a service that your application can access under explicit permissions.

We will use a private Supabase table and a server function. The browser sends a small message; the server validates it and writes the row. This lesson does not expose a public endpoint that lists everyone’s messages.

Worked example

After sending “Database test D1”, close the page and find that exact message in your project’s table editor. A message displayed only in the page is not storage evidence.

Supabase: database

Extra implementation exercise

Your page shows the submitted message from a local variable. Does that prove the database saved it?

Show a worked solution

No. Verify the actual row in the database. The browser could display the input even if no server request happened.

  • Separates displayed state from storage.
  • Checks the matching row through an authorized database view.

Step 2

Prepare the isolated storage project

Have the assistant apply the supplied setup safely.

Create or choose your disposable Supabase project. Ask the assistant to review and help apply schema.sql there, configure the private values and run the complete project using run-locally.txt.

Confirm the correct project before applying changes. Keep public visitors unable to browse all feedback; use the owner’s Table Editor to inspect the test row.

A concrete example

The supplied feedback table has a message, a generated ID and a creation time. The project owner can inspect it in the provider; visitors do not need permission to read everyone’s feedback for this test to work.

Supabase: row-level security · Node.js: supported release status

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Create a private practice table

Enable row-level security and keep direct browser access closed in this worked route.

Run this schema only in the isolated practice Supabase project named in your setup. Browser access is intentionally restricted; verify the row as the project owner in Dashboard → Table Editor. Do not run this lab schema against a production database.

Create a separate Supabase practice project and run schema.sql in its SQL editor. The table has a generated ID, a constrained message and a server timestamp. Do not run sample schema changes against an unrelated production database.

The script enables row-level security and removes table privileges from browser roles. The server’s secret/service-role key can perform the privileged write; it must remain server-side. A publishable browser key is different and does not grant these administrative permissions.

Open Table Editor in the Supabase project dashboard to inspect your practice row; this is not the browser's developer console. For a failed insert, inspect the server response and the project's database/schema configuration. Do not enable anonymous SELECT merely to see whether an insert worked.

For the complete local sequence, open run-locally.txt: prepare the private root environment, link only the practice project and start the Netlify function proxy. server-env.example.txt lists variable names without real secrets. Opening only the HTML cannot run the server.

Worked example

No broad “allow all” policy is needed for this server-mediated lab. Opening anonymous SELECT to diagnose an error would also expose submitted messages.

create table public.feedback (
 id uuid primary key default gen_random_uuid(),
 message text not null check (char_length(message) between 1 and 1000),
 created_at timestamptz not null default now()
);
alter table public.feedback enable row level security;
revoke all on public.feedback from anon, authenticated;
grant insert on public.feedback to service_role;
-- No browser read/write policies: the practice server uses its secret key.

Supabase: row-level security · Node.js: supported release status

Extra implementation exercise

A browser query is denied. Should you fix it by allowing anonymous users to read the whole feedback table?

Show a worked solution

No. This design intentionally denies browser reads. Check the server’s write setup and inspect the table as its owner.

  • Preserves the intended private-data boundary.
  • Uses the server route or authorized dashboard rather than opening global reads.

Step 3

Submit a record from the page

Let the ready server handle the write.

Open the real local or hosted practice app and submit a message such as D1. Ask your assistant to keep only intended visitor fields in the saved record.

Find the exact D1 text in the provider table and inspect its generated identity/time. Keep secret account values out of screenshots or shared notes.

A concrete example

Submit D1, then find that exact text in feedback. The generated ID distinguishes this row from an old message with similar wording; the visitor should not choose or overwrite that identity.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Connect a validated server write

The server chooses the table and permitted fields; the caller supplies only the message.

Configure the three named server variables and deploy or run the separate function project. The tester enters the practice access code. It authorizes this restricted endpoint, not the Supabase account.

Read mini-feedback.mjs: the function checks method, access code, size and message before calling the provider. It ignores caller-supplied IDs or table names and returns a storage success only after a successful insert response.

Worked example

A request containing an extra admin field does not become an administrative update: the server constructs a new object containing only message.

Extra implementation exercise

Why construct {message} instead of forwarding the entire browser JSON object to the database?

Show a worked solution

It keeps the server’s accepted schema explicit and prevents unexpected fields from becoming writes. Validate the message too; selecting fields alone is not sufficient.

  • Limits the writable fields.
  • Does not trust client-controlled IDs, timestamps or privileges.

Step 4

Check persistence and failure

Repeat the action after closing the first page.

Close and reopen the app, then verify D1 still exists in storage. Try missing/oversized input and a wrong practice code in the disposable setup.

Invalid attempts must not create new rows. A previous success message must not remain as if a later failed attempt also saved.

A concrete example

Record the valid row before the negative tests, then inspect the table afterward. “The form showed an error” and “no invalid row was added” are two useful parts of the check.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Verify persistence and failures

Use distinctive records and inspect the database independently of the page.

Send D1 with the correct practice code and inspect its matching row in the table editor. Close and reopen the page, then confirm the row still exists. Record the row’s ID and timestamp without sharing account secrets.

Try a missing message, an oversized message and an incorrect access code. None should create a new row. In a disposable setup, check that missing server configuration produces an honest failure.

Worked example

A valid record plus two rejected invalid requests gives more information than three repeated successful clicks. Avoid using real personal feedback for a first lab.

Extra implementation exercise

The endpoint returns an error, but the browser still says “Saved” from a previous attempt. What should you verify?

Show a worked solution

Check whether the new value has a row. The UI should show the current failure, and an earlier success must not be counted as this attempt’s result.

  • Checks the current response and a distinctive new test value.
  • Does not reuse an old success message as evidence for a failed request.

Step 5

Add a topic without opening the data

Use an assistant for the field change and inspect the result.

Ask the assistant to add two topics, Project and Question, to the form, storage setup and server validation. Their stored values are project and question. The supplied reference is a comparison to open after your own attempt, not the place you must discover the requirement.

Create one controlled record per topic and try an unsupported topic, other, with the assistant’s help. Verify both saved values and the rejection, keeping the feedback table private to visitors.

A concrete example

Add two allowed topics, Project and Question. Each chosen value should be stored beside its message. Other is a rejection test, so an attractive selector alone does not complete the storage change.

Files for this step

Start hereWorkshop files

Open or download the files below. When saving a project, keep the folder paths shown in the filenames.

How it works (optional)

Your assignment: store a validated topic

Extend the data model and server together without opening browser permissions.

Add a topic column constrained to project or question. Update the server to validate the allowlist and construct the inserted object explicitly. Add the corresponding form control.

Create one record per allowed topic and reject an unsupported value. Keep generated IDs and timestamps under database control.

DEFAULT assigns question to existing rows in this exercise. Without a value/default, adding a NOT NULL column can fail when rows already exist. In a real migration, choose the backfill value according to the data rather than copying this lab default.

Worked example

The database constraint backs up the server validation. Neither replaces the other’s role in clear errors and data integrity.

The complete reference walkthrough is available in reference/assignment-walkthrough.txt. Compare its steps with your own result; fill the test record from observations in your own project.

-- Existing rows need a value when the new column is NOT NULL.
alter table public.feedback add column topic text not null default 'question';
alter table public.feedback add constraint feedback_topic_allowed
 check (topic in ('project','question'));

Extra implementation exercise

Add the topic field and prove that valid data persists while invalid data is rejected.

Show a worked solution

The topic extension requires all three layers to agree: a browser select, the server allowlist and a database column/check constraint. The reference ALTER TABLE handles the starter's existing rows by assigning the documented default topic before enforcing the constraint.

The server writes only {message,topic}. It must reject an unsupported topic before using the secret database credential. It must also ignore caller-supplied IDs/table names and report a provider write failure as failure.

Inspect the exact saved message/topic in Supabase's dashboard Table Editor, not a client-side success paragraph. Reload or close the browser and verify the same stored record. Use account-owned diagnostics; do not expose anonymous reads to troubleshoot.

  • Topic validation exists in the server and database.
  • Matching valid rows persist and unsupported values do not create rows.

Was this lesson useful?

Leave a like, or share it with someone who could use it.

Continue learning

Explore the other lessons