Posted by: phillipnb | June 13, 2012

Creating a form in CodeIgniter

Click here for the CodeIgniter Multiple Choice and Interview Questions Ebook

Many people have asked me about how to create a form using CodeIngiter framework. Also, creating forms using CodeIgniter is much easier than creating a form just by using PHP. Considering these two, in this month’s edition of this blog we will create a form using PHP and CodeIgniter.

Though CodeIgniter has its own syntax for forms, ultimately this syntax is converted to html for processing the form by the Web server.Let us take an example. In this example form we are going to have five different user inputs. We am going to ask the user to enter his/her name, nationality, gender, favorite colors and preferred mode(s) of transport. Here is the code for that: (I am going to assume that you have your CodeIgniter environment ready.)

$name = array('id' => 'name_id','name' => 'full_name','size' => '20');				
$nationality = array('id' => 'nationality_id','name' => 'nationality','size' => '20');	
$gender = array('id' => 'gender_id', 'name' => 'gender');
$transport = array('id' => 'transport_id','name' => 'transport');															
$favourite = array('id' => 'favourite_id','name' => 'favourite');				

$nationality_options = array('0' => 'Please Select', '1' => 'Chinese', '2' => 'Indian', '3' => 'American', '4' => 'Others');
$favourite_options = array('0' => 'Orange', '1' => 'Red', '2' => 'Blue','3' => 'Green', '4' => 'Others');				
$attributes = array('style' => 'font-size:13px;');

echo form_open('test_form_uri');
echo '<br/>';
echo form_label('Name:',$name['id'], $attributes);
echo form_input($name);
echo '<br/>';
echo form_label('Nationality:',$nationality['id'],$attributes);
echo form_dropdown($nationality['name'],$nationality_options);
echo '<br/>';
echo form_label('Gender:',$gender['id'],$attributes);
echo form_radio($gender,'male');
echo '<span style="font-size:12px;">Male</span>';
echo '&nbsp;';
echo form_radio($gender,'female');
echo '<span style="font-size:12px;">Female</span>';
echo '<br/>';
echo form_label('Preferred Mode(s) of Transport:','',$attributes);
echo form_checkbox($transport,'air');
echo '<span style="font-size:12px;">By Air</span>&nbsp;';
echo form_checkbox($transport,'train');
echo '<span style="font-size:12px;">By Train</span>&nbsp;';
echo form_checkbox($transport,'car');
echo '<span style="font-size:12px;">By Car</span>';
echo '<br/>';
echo form_label('Favorite Colors:','',$attributes);
echo form_multiselect($favourite['name'],$favourite_options,'0');
echo '<br/>';
echo form_submit('submit','Submit');
echo form_close();

Now, let us disset the code. The function form_open() creates an opening form tag. This function gets converted to ‘form’ tag in html when this form gets processed. To create a input to allow the user to type in a value for the ‘name’ field, we will use form_input() function. It actually generates a standard text input field. The function form_label() helps to create ‘label’ element of html. The function form_radio() helps us to generate the input of type radio or a radio button element while the form_checkbox() helps us to create a check box field. The function form_multiselect() helps to create a multiselect field. The function form_submit() creates a submit button.

In case of form_input() for the ‘Name’ field, the ‘$name’ array which holds the attributes like id, name, size etc is passed as a parameter. In case of form_dropdown, the first parameter of this function is the ‘name’ attribute while the options for the drop down is passed as the second parameter. In case of form_radio() function, the first parameter holds the attribute while the second parameter stores the value associated with this radio button. The same logic is followed in case of form_checkbox(). Finally, we close the form using the function form_close(). If you would like to know more details about these functions then please read the CodeIgniter User Guide.

The only thing that remains is – what happens when we submit this form? Well, take a look at the function called form_open(). The parameter that we find there is ‘test_form_uri’. The first parameter of form_open() is the ‘action’ attribute and that tells where should this form be submitted to. The parameter ‘test_form_uri’ is set in the config file to the appropriate method/function of our controller. Make sure that we load the config file using the loader class syntax.e.g: $this->load->config(‘config_file_name’) and also resolve ‘test_form_uri’ using $this->config->item(‘test_form_uri’).

The output for the above form will look like this:

ci form

I hope that was helpful. Keep programming in PHP



  1. this is really helpfull for me.. Thanks

  2. could u show us the controller and model code of this example?

  3. Why is it that “air’ is unchecked, but the other 2 are checked – how do you differentiate a checked from an unchecked checkbox?

    • Why is “air” unchecked?
      Answer – that is just an example. You can check or uncheck whatever you want.

      How to differentiate checked and unchecked?
      Answer – from the example in the CI user guide….
      echo form_checkbox(‘newsletter’, ‘accept’, TRUE);
      The third parameter contains a boolean TRUE/FALSE to determine whether the box should be checked or not.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: