Documentation · Chapter 04

Make a form
communicate.

A form is a dialogue: the server displays fields, the user fills them, then the browser sends the values back to the server.

We continue the reading-list project. Data will be kept in the session before introducing a database.

≈ 90 min

We will make the library editable.

The previous chapter displayed books written in the controller. We will now add, correct, and delete books from the browser. Every action remains in PHP and uses PHPAML protections.

Two request types are enough to begin

GET

Request a page or read a resource. A normal browser visit uses GET.

POST

Send values that cause a change, such as adding a book.

GET should be repeatable without changing the library. POST expresses an intent to change. This separation makes the application predictable.

The new journey

1GET/books/create
→
2Formcreate.php
→
3POST/books
→
4Redirect/books

After a successful POST, the server does not directly render the list: it asks the browser to visit /books. This POST–Redirect–GET pattern avoids creating the same book twice when the page is refreshed.

VOCABULARY

Words to remember

FieldArea where a user types or selects a value.
ValidationCheck that values follow expected rules.
SessionSmall data space associated with one browser across requests.
RedirectResponse asking the browser to visit another address.

SUMMARY

What to remember

  • GET reads; POST requests a change.
  • A field name becomes the submitted key.
  • Received data is never trusted by default.
  • POST–Redirect–GET prevents accidental repetition.