Skip to content

How it works ​

A tool is a script that runs from top to bottom. Every time the user changes an input or presses a button, the browser sends the whole form and the script runs again. Each function draws its part of the page and returns what the user did with it.

The browserScript UIYour scriptthe whole form: every input,and the button that was pressedchecks the login, the tooland the token of the formruns it from the toptext('Customer email')what the user typedbutton('Refund')true, in this run onlydoes the work:your code, your datathe end of the scriptthe new pageswaps the content, no reload
One click, from start to end. Every interaction sends the whole form, and the script runs again from the top.

What the functions return ​

You writeYou get
text('Customer email')what the user typed
select('Status', ['paid', 'shipped'])one of your options, whatever the browser sent
table($rows, selectable: true)the rows the user ticked
file_input('Orders', types: ['csv'])the uploaded file, on every run until it is removed
button('Save')true in the run that its click started, and only there
button('Delete', confirm: 'Delete 3 rows?')true only after the user answered yes to exactly that question

All functions has the rest.

Three things that follow ​

The order of the code is the order of the page. What you call first stands at the top. An if around a part decides whether it is there. An early return ends the page.

Data changes behind a button. The script also runs when somebody only changes a filter, and when a page refreshes by itself. Work that changes something belongs inside if (button(...)), which is true once for each click.

php
if (button('Refund', confirm: 'Refund order ' . $order['id'] . '?')) {
    $refunds->create($order['id']);   // runs once, after a click and a yes
    success('Refunded.');
}

The script keeps nothing between two runs. A variable is gone when the run ends. What the user entered comes back through the input functions, because the library saves the inputs of every tool in the session. Everything else comes from your database, as in any PHP request.

When the script runs again ​

The userThe script runs again
chooses an option, ticks a box, picks a date, changes a numberat once
types into a text fieldwhen they press Enter or leave the field
types into the search box of a tableafter a short pause in the typing
presses a buttonat once, and button() is true in that run
does nothing, on a page that calls refresh(30)every 30 seconds, while the page is in view. It waits while the user is in the middle of something, and pauses after an hour that nobody touched it

What the user notices ​

  • The page updates in place. With JavaScript only the content is swapped, and the keyboard focus stays where it was. Without JavaScript the page works as a plain form.
  • The inputs survive a reload. The link "Reset" in the header forgets them.
  • After an action, rerun() runs the script once more, so that a table above the button shows the new data. Messages written after the click are kept.

The names of inputs ​

Every input and every button has a key. It names the form field and the saved value, and it is made from the label: 'Customer email' becomes customer-email. Two inputs with the same label on one page are an error until each has a key of its own, as in text('Name', key: 'billing-name'). In a loop, build the key from the row:

php
foreach ($orders as $order) {
    if (button('Refund', key: 'refund-' . $order['id'])) {
        $refunds->create($order['id']);
    }
}

The README says why keys are not simply numbered.

Where to go from here ​

Released under the MIT License.