form_textarea()

function form_textarea(string $name, ?string $value = null, array $attributes = []): string

Description

Generates an HTML textarea element for multi-line text input. Textareas are ideal for longer content like comments, descriptions, or messages.

Parameters

Parameter Type Description
$name string The name attribute for the textarea element.
$value string|null (optional) The initial text content of the textarea.
$attributes array (optional) HTML attributes for the textarea element. Defaults to an empty array ([]).

Return Value

Type Description
string An HTML textarea element with the specified content and attributes.

Example #1: Basic Textarea

Example #2: Textarea with Pre-filled Content

Useful for editing existing content, like in a CMS or admin panel:

Example #3: Textarea with Size Attributes

Example #4: Resizable Textarea with Character Limits

Common Textarea Attributes

Attribute Purpose Example
rows Visible number of text lines rows="5"
cols Visible width in characters cols="80"
maxlength Maximum character count maxlength="1000"
minlength Minimum character count minlength="10"
placeholder Hint text when empty placeholder="Type here..."
readonly Prevents text modification readonly="readonly"
disabled Disables the textarea disabled="disabled"

**Content Safety:** All textarea content is automatically HTML-escaped using htmlspecialchars() to prevent XSS vulnerabilities.

  • Use rows and cols for basic size control
  • Consider CSS width and height for more precise sizing
  • Add maxlength to prevent excessively long submissions
  • Use placeholder text to guide users on expected content
  • Textareas preserve whitespace and line breaks from user input
  • Retrieve submitted content with post('field_name', true)
  • For rich text editing, consider JavaScript WYSIWYG editors

Practical Example

Here's how you might use a textarea in a contact form:

Usage Notes

  • The $value parameter is ideal for pre-populating editable content (like in edit forms)
  • For hint text that disappears when typing, use the placeholder attribute instead
  • Textareas automatically preserve line breaks and formatting from user input
  • Consider using nl2br() when displaying textarea content to preserve line breaks
  • For very long content, consider implementing a character counter with JavaScript