Textareas and Dropdowns
Two more essential form elements: multi-line text and select menus.
Textareas
Use for multi-line text input:
echo form_textarea('description');Output:
<textarea name="description"></textarea>With a Default Value
$default_text = 'Enter your message here...';
echo form_textarea('message', $default_text);With Attributes
$attributes = [
'rows' => '8',
'placeholder' => 'Enter detailed description...',
'maxlength' => '500'
];
echo form_textarea('description', '', $attributes);Repopulating After Validation Errors
Best Practice: Always pass form data from controller to view. The controller should fetch submitted data and pass it to the view.
echo form_textarea('description', $description);In your controller:
// Fetch submitted data or use default
$data['description'] = post('description', true);
$this->view('create', $data);If the form hasn't been submitted, this displays empty. If validation failed, it shows what the user typed.
Dropdowns (Select Menus)
Use to create select menus. The pattern is slightly different:
form_dropdown($name, $options, $selected, $attributes);- $name - the field name (required)
- $options - array of options (required)
- $selected - the selected value (optional)
- $attributes - array of HTML attributes (optional)
Basic Example
$options = [
'1' => 'Option One',
'2' => 'Option Two',
'3' => 'Option Three'
];
echo form_dropdown('choice', $options);Output:
<select name="choice">
<option value="1">Option One</option>
<option value="2">Option Two</option>
<option value="3">Option Three</option>
</select>With a Selected Value
$options = [
'small' => 'Small',
'medium' => 'Medium',
'large' => 'Large'
];
echo form_dropdown('size', $options, $size);Controller prepares the data:
// In controller
$data['size'] = post('size', true) ?? 'medium'; // Default to 'medium'
$this->view('create', $data);The appropriate option will be selected based on the $size variable.
With Attributes
$options = [
'1' => 'Priority 1 - Critical',
'2' => 'Priority 2 - High',
'3' => 'Priority 3 - Normal'
];
$attributes = ['id' => 'priority-selector', 'required' => 'required'];
echo form_dropdown('priority', $options, $priority, $attributes);Real-World Example: Status Selector
// In controller
$status_options = [
'pending' => 'Pending',
'in_progress' => 'In Progress',
'completed' => 'Completed',
'canceled' => 'Canceled'
];
$data['status_options'] = $status_options;
$data['status'] = post('status', true); // Gets submitted value or empty string
$this->view('create', $data);// In view
echo form_label('Status');
echo form_dropdown('status', $status_options, $status);Building Options from a Database
The most common pattern is building dropdown options from database records. The controller builds the options array and passes it to the view:
// In controller
$categories = $this->db->get('id', 'categories');
$category_options = [];
foreach ($categories as $category) {
$category_options[$category->id] = $category->name;
}
// Pass both variables to view
$data['category_options'] = $category_options;
$data['selected_category'] = post('category_id', true);
$this->view('create', $data);// In view - $category_options and $selected_category come from controller
echo form_label('Category');
echo form_dropdown('category_id', $category_options, $selected_category);Adding a Blank Option
Start with an empty or "please select" option:
$options = [
'' => '-- Please Select --',
'1' => 'Option One',
'2' => 'Option Two',
'3' => 'Option Three'
];
echo form_dropdown('choice', $options, $selected_choice);Working with Create/Update Forms
Here's the standard pattern for dropdowns that work for both creating and updating records:
// In controller
public function create(): void {
$update_id = segment(3, 'int');
// Fetch categories for dropdown
$categories = $this->db->get('id', 'categories');
$data['category_options'] = [];
foreach ($categories as $category) {
$data['category_options'][$category->id] = $category->name;
}
// Get selected value (from DB or POST)
if ($update_id > 0 && REQUEST_TYPE === 'GET') {
$record = $this->db->get_where($update_id, 'products');
$data['selected_category'] = $record->category_id;
} else {
$data['selected_category'] = post('category_id', true);
}
$this->view('create', $data);
}// In view - $category_options and $selected_category come from controller
echo form_dropdown('category_id', $category_options, $selected_category);Multiple Selections
Allow users to select multiple options:
// In controller
$data['color_options'] = [
'red' => 'Red',
'blue' => 'Blue',
'green' => 'Green',
'yellow' => 'Yellow'
];
// For multiple selections, post() returns an array
$data['selected_colors'] = post('colors', true) ?: [];
$this->view('create', $data);// In view
$attributes = ['multiple' => 'multiple'];
echo form_dropdown('colors[]', $color_options, $selected_colors, $attributes);Important notes:
- The field name must end with
[]to receive an array - Add
'multiple' => 'multiple'to attributes - On submission, returns an array of selected values
Pre-selecting Multiple Options
$selected_colors = ['red', 'blue']; // Array of values
echo form_dropdown('colors[]', $options, $selected_colors, ['multiple' => 'multiple']);Common Textarea Attributes
| Attribute | Purpose | Example |
|---|---|---|
rows |
Visible lines | ['rows' => '10'] |
cols |
Visible width | ['cols' => '50'] |
placeholder |
Hint text | ['placeholder' => 'Enter text...'] |
maxlength |
Character limit | ['maxlength' => '1000'] |
required |
HTML5 validation | ['required' => 'required'] |
Common Dropdown Attributes
| Attribute | Purpose | Example |
|---|---|---|
multiple |
Allow multiple selections | ['multiple' => 'multiple'] |
size |
Visible options | ['size' => '5'] |
required |
HTML5 validation | ['required' => 'required'] |
disabled |
Disable selection | ['disabled' => 'disabled'] |
Both and automatically escape output for XSS protection.
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.