A page with post: sql: runs one statement when it receives a post. :name stands for the posted field name, bound as a parameter. :user and :role are the signed-in visitor, if any.
With redirect, a successful post answers with a redirect, so reloading the page does not insert the line twice. Put {{ post.hidden }} inside the form you write; it carries the form token.
The guestbook below is that page. Sign it and your line appears at the top. The site's admin can hide lines in a protected office page.
Live result This page is rendered on request.
Thank you. Your line is in the guestbook.
The first line in the guestbook. Yours goes above it.
The Alcove maintainers,
No one has signed yet.
Source the files behind the result above
content/pages/docs-sql-posts.yaml
slug: docs/sql-posts
lang: en
title: SQL posts
alcove: docs
meta:
description: "One bound statement when a page receives a post."
nav: docs
eyebrow: "Part III, Interaction"
doc: sql-posts
mode: "This page is rendered on request."
post:
sql: "INSERT INTO guestbook (name, note) SELECT substr(trim(:name), 1, 60), substr(trim(:note), 1, 300) WHERE length(trim(:name)) > 0 AND length(trim(:note)) > 0"
redirect: /docs/sql-posts/?signed=1#live
nooks:
intro:
- alcove: doc_sql_posts
live:
- alcove: demo_signform
- alcove: demo_guestbook
after:
- alcove: doc_sql_posts_after
content/alcoves/demo_signform/blueprint.html
<div class="sign" data-signed="{{ query.signed }}">
<p class="thanks" role="status">Thank you. Your line is in the guestbook.</p>
<form class="sign-form" method="post" action="/docs/sql-posts/">
{{ post.hidden }}
<div class="field">
<label for="gb-name">Name</label>
<input id="gb-name" name="name" maxlength="60" required autocomplete="name">
</div>
<div class="field field-wide">
<label for="gb-note">A line</label>
<textarea id="gb-note" name="note" maxlength="300" rows="3" required></textarea>
</div>
<p class="sign-foot"><button class="btn btn-dark" type="submit">Sign</button> <span>Names and lines are public.</span></p>
</form>
</div>
content/alcoves/demo_guestbook/alcove.yaml
query: |
SELECT name, note, substr(signed_at, 1, 10) AS signed_on
FROM guestbook
WHERE shown = 1
ORDER BY id DESC
LIMIT 12