form_input()

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

Description

Generates a standard text input field with type="text". This is the most commonly used form input type.

Parameters

ParameterTypeDescription
$namestringThe name attribute for the input element.
$valuestring|null(optional) The initial text value displayed in the field.
$attributesarray(optional) HTML attributes for the input element. Defaults to an empty array ([]).

Return Value

TypeDescription
stringAn HTML text input element.

Example #1: Basic Text Input

PHP
echo form_input('username', 'John Doe');

// Output:
// <input type="text" name="username" value="John Doe">

Example #2: Text Input with Common Attributes

PHP
$attributes = [
    'placeholder' => 'Enter your name',
    'maxlength' => '50',
    'required' => 'required'
];
echo form_input('full_name', '', $attributes);

// Output:
// <input type="text" name="full_name" placeholder="Enter your name" maxlength="50" required="required">

Example #3: Form Input Pattern

PHP
// Controller pattern for create/edit forms
$update_id = segment(3, 'int');
if ($update_id > 0 && REQUEST_TYPE === 'GET') {
    $record = $this->db->get_where($update_id, 'users');
    $data['username'] = $record->username;
} else {
    $data['username'] = post('username', true);
}
$this->view('user_form', $data);
View File
// In the view
echo form_label('Username');
echo form_input('username', $username);

Add properties like placeholder, maxlength, required, and pattern for better user experience and validation.

Notes

  • Retrieve submitted values with post('field_name', true).
  • Use maxlength attribute to limit input length client-side.
  • Add required attribute for HTML5 client-side validation.
  • Combine with form_label() for accessible form fields.
  • For specialized input types, use specific functions like form_email(), form_number(), etc.