The form from the hx-post lesson already sends an order without a reload. But after Mrs. Tini's order is recorded, her name and weight are still sitting in the boxes, so the cashier has to delete them by hand before the next customer. The "use fragrance" note typed in the box on the side, outside the form, never reaches the server either.
This lesson sorts out three things that often confuse people in a form with hx-post: which fields get sent, how to bring a field from outside the form with hx-include, and how to clear the form after a success with hx-on::after-request. The form still sends its content through htmx, without reloading the page, and the HTML reply goes into the target. It's like the slip at Mrs. Rini's Laundry counter: only the named boxes inside the slip are copied into the order book. A note on a sticky paper beside the slip is left behind, unless you staple it to the slip. As soon as the order is recorded, Mrs. Rini tears off a fresh blank slip for the next customer.
A form with hx-post
<form id="intake" hx-post="/order" hx-target="#order-board" hx-swap="beforeend">
<input name="name" placeholder="Name">
<input name="weight" placeholder="kg">
<label><input type="checkbox" name="express" value="yes"> Express</label>
<button>Take in laundry</button>
</form>
- Pressing Take in laundry, or Enter in one of the boxes, sends
POST /order. - The body is in
application/x-www-form-urlencodedformat, the same as a plain HTML form:name=Mrs.%20Tini&weight=4. - The server replies with one row,
<li id="order-15">#15 Mrs. Tini, 4 kg: <span>Queued</span></li>, andhx-swap="beforeend"adds it at the end of the board. - The browser's own validation still runs first: an empty
requiredbox means the request isn't sent at all.
Which fields get sent
| Field | Sent? |
|---|---|
<input name="name"> inside the form |
yes |
a field with no name |
no |
a disabled field |
no |
a readonly field |
yes |
| an unchecked checkbox | no. A checked one sends express=yes (with no value: express=on) |
the submit button that was clicked, if it has a name |
yes: <button name="service" value="same-day"> sends service=same-day |
a field outside the <form> |
no, unless hx-include brings it |
hx-include: bring a field from outside the form
The Notes box sits on the side, outside the form. hx-include staples it to what's sent:
<input id="notes" name="notes" value="use fragrance">
<form id="intake" hx-post="/order" hx-target="#order-board" hx-swap="beforeend"
hx-include="#notes">
...
</form>
- The body becomes
notes=use%20fragrance&name=Mrs.%20Tini&weight=4. In a POST form, the fieldshx-includebrings go first. - Its value is a CSS selector (use
#for an id) or a keyword such asclosest form. If it points at a<div>, every field inside it goes along. - An element that isn't a form can use it too:
<button hx-get="/order" hx-include="#filter">sendsGET /order?filter=active.
Clearing the form after a success
<form id="intake" hx-post="/order" hx-target="#order-board" hx-swap="beforeend"
hx-on::after-request="if (event.detail.successful) this.reset()">
hx-on::after-requestruns a short piece of JavaScript as soon as the request is done. The double colon is short forhx-on:htmx:after-request.- Inside it,
thisis the form, andevent.detail.successfulistruewhen the reply is a 2xx. this.reset()on its own also runs after a failed request. The server replies 422, and the form is cleared anyway: "Mrs. Tini" is gone too, and the cashier has to type everything again. That's why you checkevent.detail.successfulfirst.reset()brings each box back to thevalue="..."in its HTML, which isn't always empty.<input name="name" value="Mrs. Tini">goes back to "Mrs. Tini".- Another way: the server replies with a fresh empty form, with
hx-target="this"andhx-swap="outerHTML"on the form. The old form is swapped for the new one.
Still working without JavaScript
<form action="/order" method="post"
hx-post="/order" hx-target="#order-board" hx-swap="beforeend">
If htmx fails to load (a slow connection, a blocked script), the browser sends this form like any form, to its action, and the server replies with the whole page. With htmx, the same route only needs to reply with one <li> when the request carries the HX-Request: true header. This is progressive enhancement: the form keeps working without JavaScript, and htmx just makes it smoother.
⚠️ Forms that clear themselves, and fields left behind
this.reset()without checking for success. The form is cleared even when the server refuses. Useif (event.detail.successful) this.reset().- A single colon.
hx-on:after-requestwaits for an event namedafter-request, which htmx never sends. Write two:hx-on::after-request. In the exercise, the form needs anhx-on::after-requestthat checksevent.detail.successful,hx-post="/api/items",hx-target="#items"andhx-swap="beforeend". - Forgetting
hx-swap="beforeend". The default isinnerHTML, so a new item wipes the old list. - A field outside the form, or
hx-includewithout#.hx-include="filter"looks for a<filter>tag, so nothing comes along. In the exercise, the button needshx-get="/api/todos"andhx-include="#filter".
TL;DR: A form with hx-post sends the named fields inside it as application/x-www-form-urlencoded. Fields with no name, disabled fields and unchecked checkboxes stay behind, but the submit button that was clicked goes along if it has a name. hx-include="#notes" brings a field from outside the form. hx-on::after-request="if (event.detail.successful) this.reset()" clears the form only after a 2xx reply, because this.reset() alone also runs on a failure. Write action and method too, so the form keeps working without JavaScript.
⚡ Which fields get sent
Write:
<form id="intake" hx-post="/order" hx-target="#order-board" hx-swap="beforeend">
<input name="name" placeholder="Name">
<input name="weight" placeholder="kg">
<label><input type="checkbox" name="express" value="yes"> Express</label>
<button>Take in laundry</button>
</form>
Guess first: the Express checkbox isn't checked. What is sent for that field?
Nothing. An unchecked checkbox doesn't go along. A checked one sends express=yes.
Meaning: Only the named fields inside the form that aren't disabled are sent. The submit button that was clicked goes along if it has a name.
⚡ hx-include: bring a field from outside the form
Write:
<input id="notes" name="notes" value="use fragrance">
<form id="intake" hx-post="/order" hx-target="#order-board" hx-swap="beforeend"
hx-include="#notes">
...
</form>
Meaning: The body becomes notes=use%20fragrance&name=Mrs.%20Tini&weight=4. The value of hx-include is a CSS selector, so an id is written with #.
⚡ Clear the form only on a success
Write:
<form id="intake" hx-post="/order" hx-target="#order-board" hx-swap="beforeend"
hx-on::after-request="if (event.detail.successful) this.reset()">
Meaning: this.reset() on its own also runs when the server refuses, so check event.detail.successful first. It takes two colons: hx-on::after-request.
⚡ Still working without JavaScript
Write:
<form action="/order" method="post"
hx-post="/order" hx-target="#order-board" hx-swap="beforeend">
Meaning: If htmx fails to load, the browser sends this form like any form, to its action. This is progressive enhancement: the form keeps working without JavaScript.