The form helper

The Form Helper file contains functions that assist in working with forms.


echo form_open('email/send');

// Would produce:
<form method="post" accept-charset="utf-8" action="http:/" />

Attributes can be added by passing an associative array to the second parameter, like this:

$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);

The above example would create a form similar to this:

<form method="post" accept-charset="utf-8" action="http:/"  class="email"  id="myform" />


Lets you generate a standard text input field. You can minimally pass the field name and value in the first and second parameter:

echo form_input('username', 'johndoe');

Or you can pass an associative array containing any data you wish your form to contain:

$data = array(
    'name' => 'username',
    'id' => 'username',
    'value' => 'johndoe',
    'maxlength' => '100',
    'size' => '50',
    'style' => 'width:50%',

echo form_input($data);

// Would produce:

<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" style="width:50%" />


Lets you create a standard drop-down field. The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. You can also pass an array of multiple items through the third parameter, and CodeIgniter will create a multiple select for you. Example:

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',

$shirts_on_sale = array('small', 'large');

echo form_dropdown('shirts', $options, 'large');

// Would produce:

<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>

echo form_dropdown('shirts', $options, $shirts_on_sale);

// Would produce:

<select name="shirts" multiple="multiple">
<option value="small" selected="selected">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>

If you would like the opening <select> to contain additional data, like an id attribute or JavaScript, you can pass it as a string in the fourth parameter:

$js = 'id="shirts" onChange="some_function();"';

echo form_dropdown('shirts', $options, 'large', $js);

Adding Hidden Input Fields

Hidden fields can be added by passing an associative array to the third parameter, like this:

$hidden = array('username' => 'Joe', 'member_id' => '234');

echo form_open('email/send', '', $hidden);

In order to use the hidden variable, open the controller and:

$username = $this->input->post('username');

$member_id = $this->input->post('member_id');


Lets you generate a checkbox field. Simple example:

echo form_checkbox('newsletter', 'accept', TRUE);

// Would produce:
<input type="checkbox" name="newsletter" value="accept" checked="checked" />

The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.

Similar to the other form functions in this helper, you can also pass an array of attributes to the function:

$data = array(
    'name'        => 'newsletter',
    'id'          => 'newsletter',
    'value'       => 'accept',
    'checked'     => false,
    'style'       => 'margin:10px',

echo form_checkbox($data);

// Would produce:
<input type="checkbox" name="newsletter" id="newsletter" value="accept" checked="checked" style="margin:10px" />

If you're dealing with multiple checkboxes, then you should use groups. Check this article for more.

echo form_checkbox('group[0]', 1, TRUE);
echo form_checkbox('group[1]', 1, TRUE);
echo form_checkbox('group[2]', 1, TRUE);


Lets you generate a standard submit button. Simple example:

echo form_submit('mysubmit', 'Submit Post!');

// Would produce:
<input type="submit" name="mysubmit" value="Submit Post!" />

Similar to other functions, you can submit an associative array in the first parameter if you prefer to set your own attributes.

$data = array(
    'id' => 'my_submit_btn_id',
    'value' => 'Submit'

echo form_submit($data);

// Would produce:

<input type="submit" id="my_submit_btn_id" value="Submit" />


echo form_submit('login/submit', 'Login', 'id="my_submit_btn_id"');

The third parameter lets you add extra data to your form, like JavaScript.


Produces a closing </form> tag.

See the original documentation page.

Creating a delete button with a confirmation message

$attributes = array('onSubmit' => "return confirm('Are you sure you want to delete the selected item?');");
echo form_open('users/delete_user', $attributes, $hidden);
echo form_submit('submit', 'delete');
echo form_close();


Post A Comment

Anti-Spam Quiz: