Forms with htmx

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…

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>

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>

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()">

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

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.

What you will learn