tom_select_box(), tags_box(), tom_select_tags(), tom_select_query(), tom_select_send() html()

Tom Select without jQuery: a searchable select (tom_select_box()), a tags input (tags_box()), and the server helpers of tags and remote search.

html()->tom_select_box($name, $values = [], $selected = null, $extra = [])
html()->tags_box($name, $values = [], $selected = null, $extra = [])
html()->tom_select_tags($value, $max_tags = 0, $max_length = 0)
html()->tom_select_query($param = 'q', $max_length = 0)
html()->tom_select_send($items, $limit = null)

Options

OptionMeaning
tom_select_box($name, $values, $selected, $extra)placeholder, allow_clear, multiple, max_items, ajax_url / ajax_param / ajax_min / ajax_limit / ajax_labels, config, disabled, required, class_add, id, attr.
tags_box($name, $values, $selected, $extra)Suggestions in $values; max_tags, tag_max_length (50), create (false: suggestions only); posts one comma-separated string.
tom_select_tags($value, $max_tags, $max_length)The posted tags string as a clean list: trimmed, unique, limits enforced.
tom_select_query($param, $max_length)$_GET[$param] of a remote search: trimmed, control characters removed, cut. Escape it for SQL yourself.
tom_select_send($items, $limit)Answer a remote search with [{value, text}] JSON (nosniff, no-store) and end the request.

Example

tom_select_tags(): ["php","htmx","<b>x</b>"]

tom_select_query('q', 50) of this request: ""

Code

$tags = html()->tom_select_tags(' php, htmx,php, <b>x</b> ,' . str_repeat('y', 80), 5, 20);
return html()->tom_select_box('country', ['ua' => 'Ukraine', 'pl' => 'Poland', 'de' => 'Germany'], 'pl', ['placeholder' => 'Pick a country'])
    . html()->tags_box('tags', ['php', 'htmx', 'mysql'], 'php,htmx', ['max_tags' => 5])
    . '<p>tom_select_tags(): <code>' . _prepare_html(json_encode($tags)) . '</code></p>'
    . '<p>tom_select_query(\'q\', 50) of this request: <code>' . _prepare_html(json_encode(html()->tom_select_query('q', 50))) . '</code></p>';

Notes

  • tom_select_send() sends headers and exits, so the example only names it: the remote search of the demo calls html()->tom_select_send($rows, 20) in its endpoint.
  • Labels are text everywhere: escaped in the markup, reduced to string value / text in a remote answer, cut at TEXT_MAX_LENGTH.

Bootstrap 3 and 5

Theme bootstrap5 under Bootstrap 5, default with Bootstrap 3 fixes otherwise; both follow the dark colour mode and the internal themes.

See also: form/tom_select_box, demo:form/tom_select