form_search()
function form_search(string $name, ?string $value = null, array $attributes = []): string
Description
Generates an HTML search input field with type="search". Modern browsers provide enhanced search features including a clear button, specialized mobile keyboards, and sometimes integrated history.
Parameters
| Parameter | Type | Description |
|---|---|---|
| $name | string | The name attribute for the search input element. |
| $value | string|null | (optional) The initial search query value displayed in the field. |
| $attributes | array | (optional) HTML attributes for the search input element. Defaults to an empty array ([]). |
Return Value
| Type | Description |
|---|---|
| string | An HTML search input element with type="search". |
Usage Examples
Basic Search Field
Search with Placeholder and Constraints
Pre-populated Search Field
When redisplaying a form after submission, you can retain the user's search term:
Complete Search Form (GET Method)
Search forms typically use GET method for bookmarkable URLs:
Note about browser differences:
- Most desktop browsers add a clear (×) button when text is entered
- Mobile devices often show a "search" keyboard with a dedicated search button
- Safari and Chrome may round the corners of search inputs by default
- Some browsers maintain a search history for the field
- **Use GET for searches:** Allows users to bookmark results and share URLs
- **Provide clear placeholders:** Guide users on what to search for
- **Set reasonable limits:** Use
minlengthandmaxlengthattributes - **Consider accessibility:** Add
aria-labelif label isn't adjacent - **Retain search terms:** Use
post('field', true)to repopulate after submission - **Handle empty searches:** Validate that searches aren't just whitespace