[Form] Forms in CodeIgniter

| | 1 min read

Forms are probably essential part in websites. Because forms are creating the user interface to collect data from users. In CodeIgniter we can generate and handle data with the help of form helper.

Form helper called by using the following code,

$this->load->helper('form');

You can generate forms and handle form activities using available functions in form helper.

echo form_open('user/add');

The above code would create a form

form method="post" action="http://yoursite.com/index.php/user/add"

For adding attributes to form you can pass it as the second parameter as array or string like,


$attributes = array('class' => 'add', 'id' => 'myform');
echo form_open('user/add', $attributes);
or
echo form_open('user/add', 'class="add" id="myform"');

Adding fields

Hidden fields

function form_hidden() helps you to add fields on your form

Eg: -

 
$data = array(
 'uid' = '0',
 'page' = 'my_page',
);
 echo form_hidden('my_array', $data); 

Add input fields

function form input will generate your input fields

Eg: -

 echo form_input('user_name', 'testname'); 

or you can use it as array

$data = array(
  'name' = 'user_name',
  'id' = 'user_name',
  'value' = '',
  'maxlength' = '100',
  'size' = '50',
  'style' = 'width:50%
);
echo form_input($data); 

Submit form

function form_submit() will generate a standard submit function

 echo form_submit('save', 'save user');

Would produce:

  

form_close() will close the form.

If you need any support, please feel free to get in touch with us.