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

PHP
echo form_textarea('comments');

// Output:
// <textarea name="comments"></textarea>

Example #2: Textarea with Pre-filled Content

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

PHP
$article_content = "Welcome to our newsletter!\n\nWe're excited to share our latest updates with you.";
echo form_textarea('content', $article_content);

// Output:
// <textarea name="content">Welcome to our newsletter!

// We're excited to share our latest updates with you.</textarea>

Example #3: Textarea with Size Attributes

PHP
$attributes = [
    'rows' => '6',
    'cols' => '50',
    'placeholder' => 'Enter your message here...'
];
echo form_textarea('message', '', $attributes);

// Output:
// <textarea name="message" rows="6" cols="50" placeholder="Enter your message here..."></textarea>

Example #4: Resizable Textarea with Character Limits

PHP
$attributes = [
    'rows' => '8',
    'maxlength' => '500',
    'minlength' => '10',
    'required' => 'required',
    'style' => 'resize: vertical;'
];
$existing_content = post('description', true);
echo form_textarea('description', $existing_content, $attributes);

// Output (with content):
// <textarea name="description" rows="8" maxlength="500" minlength="10" required="required" style="resize: vertical;">Existing content...</textarea>

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:

View File
<?php
echo form_open('contact/submit');
echo form_label('Your Message');
echo form_textarea('message', post('message', true), [
    'rows' => '6',
    'placeholder' => 'Please describe your inquiry in detail...',
    'required' => 'required'
]);
echo form_submit('send', 'Send Message');
echo form_close();
?>

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