Codeigniter tutorial

In our previous tutorials we learned about all functions of codeigniter that we can use to generate different elements for our form UI, but when we create a form it is also important that we know how to process the user inputs. In this tutorial we will create a form and process all inputs to our form, so let us begin.

Previous tutorials:

Codeigniter Form Tutorial for Beginners Part 1

Codeigniter Form Tutorial for Beginners Part 2

Codeigniter Form Tutorial for Beginners Part 3

Codeigniter Form Tutorial for Beginners Part 4


Before we start building our form and process it we should know how we access the get and post variables in codeigniter with codeigniter’s form helper.

Retrieving codeigniter’s form post data with‘$this->input->post()’

With PHP we use $_POST variable to retrieve the data submitted with post method, but in codeigniter the form helper allows us to access post data using the form helper with “$this->input->post()” method. If the element value is not set then this will return a null value. Here is a simple example.


Note: The above syntax will store the post data for “name” variable in the $data array with key as ‘name’.

Retrieving codeigniter’s form get data with ‘$this->input->get()’

As same as post variables in PHP we retrieve the get data using $_GET method, codeigniter’s form helper allows us to access get data with “$this->input->get()” method. If the element value is not set then this will return a null value. Here is a simple example.


Note: The above syntax will store the get data for “name” variable in the $data array with key as ‘name’.

Retrieving codeigniter’s form combined post and get data with ‘$this->input->post_get()’

In some occasions we may need to access values with unknown methods used, may be it is get or it is post, codeigniter’s form helper function provides a specific method of accessing both post and get data at the same time, in such case the post data is given priority whereas get data gets second priority. The property can be accessed using “$this->input->post_get()” method. Here is a simple example.


Note: The above syntax will store both post and get data for “name” variable in the $data array with key as ‘name’ where the post data will be given priority.

As we know the three codeigniter’s methods for accessing our form submission data so let us now access the data we submit with our form and print on screen, we will use post method for this form example, you can explore get() and post_get() method more by changing the form submission method to get and post as needed.

Creating and Submitting Forms with Codeigniter Tutorial for Beginners

Building Our Form View in Codeigniter

In previous tutorials we learned about building our codeigniter form elements, now let us compile our past form tutorials and create a new form for us.

Create a new view file name it user-data.php inside the codeigniter’s view directory and add the following codes in it. The following codes will generate

  • Form for creating the user form with codeigniter’s form_open() method.
  • Labels to display field names with codeigniter’s form_label() method.
  • Input elements for “Name” and “Last Name” with the form_input() method of codeigniter.
  • Drop down element for “Sex” input with the form_dropdown() method of codeigniter.
  • Text area element for user’s address with the form_textarea() method of codeigniter.
  • Submit Button for submitting all the user’s input to our script and process them with codeigniter’s form_submit() method.
$attributes=array('class'=>'my_form', 'id'=>'my_form');
$hidden=array('register'=>'1', 'user_type'=>'client');
echo form_open($action, $attributes, $hidden);
echo form_label('Enter your name:','username',array('class'=>'my_label', 'id'=>'my_name_label'));
'placeholder'=>'enter your name'
echo form_input($input_array);
echo '<br>';
echo form_label('Enter your last name:','lastname',array('class'=>'my_label', 'id'=>'my_lastname_label'));
'placeholder'=>'enter your last name'
echo form_input($input_array);
echo form_label('Select your SEX:','select',array('class'=>'my_label', 'id'=>'my_select_label'));
$option=array('0'=>'Select', 'male'=>'Male', 'female'=>'Female');
echo form_dropdown($select, $option, $selected);
echo '<br>';
echo form_label('Enter your address:','address',array('class'=>'my_label', 'id'=>'my_textarea_label'));
'placeholder'=>'Enter your address'
echo form_textarea($textarea);
'class'=>'btn btn-default',
echo form_submit($submit, 'submit');
echo form_close();
echo 'You filled in the following details<br>' . $name . '<br>'. $lastname . '<br>' . $sex . '<br>' . $address;


Note: We will later pass the submitted variables to our view file via our controller which we will print on the user’s screen.

Building our Form Controller in Codeigniter

Our form view is ready, we need is a codeigniter controller for loading the form and display it on user’s screen, then we can process it before we do further actions based on user inputs i.e. before storing in database, if there are errors we need to display them. So we will append our existing “Home” controller with a new function “register()”, then we will store the values passed by form’s post method in a array variable and pass it to our view file then print them on user’s screen. Here is my controller’s code snippet,  where I am accessing all the post values submitted by the user.

public function register()
$this->load->view('user-data', $data);


Conclusion: Now we have successfully created our form and accessed the form submission in our controller.

As by now we have created our view file and our controller, it is time to load up our controller and check in browser, you must see a blank form populated on your screen

Blank codeigniter form view

Once you have loaded the form then fill in all form fields and submit the form, you will see all post data that was filled in is now printed on your screen.

codeigniter form submit result

We have successfully accessed the post data and displayed on our screen. That’s all for today, if you found this tutorial helpful then share with your friends, like us on social media and subscribe to our notification by clicking the bell to stay updated with our newest tutorials. If you have any questions or queries write in the comments below.


Please enter your comment!
Please enter your name here