CodeIgniter Form Helper

In this tutorial, we are discussing “Form Helper”  In CodeIgniter. We generally use a form to collect input from the user and store it either database or file. And uses this information as per application requirements. In CodeIgniter, The Form Helper file contains functions that assist us working with forms more effectively.

Before using the Form Helper, we need to load the helper So, let’s begin this tutorial by learning how to load a form helper library.

We can load the “Form Helper” using two methods:

  1. Using Autoload Method
  2. Using Load Method Manual

Load Form Helper Using Autoload Method:

Open application/config/autoload.php and configure it as given below

 $autoload['helper'] = array('form');

Load Form Helper Using Load Method:

We can load the “Form Helper” using load method at your controller as follows.

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

 Syntax to define a form in Codeigniter:

form_open([$action = ”[, $attributes = ”[, $hidden = array()]]])

Parameters:
  • $action (string) – Form action/target URI string
  • $attributes (array) – HTML attributes
  • $hidden (array) – An array of hidden fields’ definitions
Returns:

An HTML form opening tag

Return type:

string

For Example:

echo form_open('email/send', 'class="email" id="myform"');

The above example creates following HTML Tag.

 <form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" class="email" id="mform">

Function with descritpion of Form Helper:

  • form_open( );  //create an opening form tag 
  • form_label( );  //create a label 
  • form_input( );  //create input field such as text , email etc. 
  • form_password( );  //create a password input field 
  • form_hidden( );  //create hidden field 
  • form_radio( );  //create radio button 
  • form_dropdown( );  //create select field 
  • form_checkbox( );  //create checkbox 
  • form_textarea( );  //create textarea 
  • form_fieldset( );  //create fieldset 
  • form_upload( );  //to upload files 
  • form_submit( );  //create submit button 
  • form_reset( );  //create reset button 
  • form_close( );  //create form closing tag 
  • set_value( ) ;  //set default value for input tag 
  • set_select( );  //set default value for select field 
  • set_checkbox();  //set default value for checkbox 
  • set_radio();  //set default value for radio button

Form Label

form_label( ) function helps to generate a simple label.

Example of form_label( ) function is given below :-

echo form_label('Employee Email-ID');

The above code will produce the following HTML tag:-

<label>Employee Email-ID</label>

 

Form Input Text

form_input( ) function allow us to generate a standard text input field. We can pass the array containing indexes and values to create different input fields.

Text input field can be created by just passing a simple associative array to form_input( ). By default form_input( ) create a text input field.

Example of text fields using form_input( ) function is given below:-

 $data_name = array(
'name' => 'emp_name',
'id' => 'emp_name_id',
'placeholder' => 'Please Enter Name'
);
echo form_input($data_name);

The above code will produce the following HTML tag:-

<input type="text" name="emp_name" value="" id="emp_name_id" placeholder="Please Enter Name"  />

Form Input with Read-only permission:-

An input field can be created with read-only permission by passing ‘readonly’ as an index and value both in the associative array of form_input() function.

An example of input field with read-only permission is given below:-

 $data_name = array(
'name' => 'emp_name',
'id' => 'emp_name_id',
'value' => 'John'
'readonly' => 'readonly'
);
echo form_input($data_name);

The above code will produce the following HTML tag:-

<input type="text" name="emp_name" value="" id="emp_name_id" value="John" readonly="readonly" />

 

Form Input Email

Email input field can be created by passing ‘type’ as index and ’email’ as a value in the array of form_input( ) function.

An example of email field using form_input( ) is given below:-

$data_email = array(
'type' => 'email',
'name' => 'emp_email',
'id' => 'emp_email_id',
'placeholder' => 'Please Enter Email'
);
echo form_input($data_email);

The above code will produce the following HTML tag :-

<input type="email" name="emp_email" value="" id="emp_email_id" placeholder="Please Enter Email"  />

 

Form Password

form_password( ) function can be used to create a password input field.All other functionality of this function is same as the form_input( ) function.

Example of form_password( ) function is given below:-

$data_password = array(
'name' => 'password',
'id' => 'password_id',
'placeholder' => 'Please Enter Password'
);
echo form_password($data_password);

The above code will produce the following HTML tag:-

<input type="password" name="password" value="" id="password_id" placeholder="Please Enter Password"  />

 

Form Input Hidden

form_hidden( ) function is used to create a hidden field.We can create a single and multiple hidden fields both using this function.

For a single hidden field, we have to pass two values in which first value contains a name and second value contains a value for the hidden field.

Example of single hidden field using form_hidden( ) function is given below :-

form_hidden('employee_name', 'John');

The above code will produce the following HTML tag:-

<input type="hidden" name="employee_name" value="John" />

To create multiple hidden fields we have to pass an associative array in which each index will act as name and their related value act as value for the hidden fields.

An example of multiple hidden fields using from_hidden( ) is given below:-

$data_hidden = array(
'employee_name' => 'John',
'employee_id' => '555'
);
echo form_hidden($data);

The above code will produce the following HTML tag :-

<input type="hidden" name="employee_name" value="John" />
<input type="hidden" name="employee_id" value="555" />

 

Form Radio Button

form_radio( ) function is used to create a radio button in a form in CodeIgniter. As we know that radio button is used for single selection so its name attribute must be same.

Example of form_radio( ) function is given below :-

$data_radio1 = array(
'name' => 'emp_marital_status',
'value' => 'Unmarried',
'checked' => TRUE,
);

$data_radio2 = array(
'name' => 'emp_marital_status',
'value' => 'Married',
);
echo form_radio($data_radio1);
echo form_radio($data_radio2);

The above code will produce the following HTML tag :-

<input type="radio" name="emp_marital_status" value="Unmarried" checked="checked"  />
<input type="radio" name="emp_marital_status" value="Married"  />

 

Form Dropdown

form_dropdown( ) function is used to create standard dropdown field. It basically contains three parameters. First parameter contains the name of the dropdown field, second parameter contains associative arrays of options needed in dropdown and the third parameter contains the value wish to be selected.

Example of form_dropdown( ) function is given below :-

$data_gender = array(
'Male' => 'Male',
'Female' => 'Female'
);
echo form_dropdown('select', $data_gender, 'Male');

The above code will produce the following HTML tag :-

<select name="select">
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>

For detailed information about form_dropdown( ) function  in codeigniter read our article  CodeIgniter form dropdown.

 

Form Checkbox

form_checkbox( ) function is used to create a checkbox field in CodeIgniter. As we know that checkbox field is used for multiple values selection so its name must be an array and each checkbox contains array having same name.

Example of form_checkbox( ) function is given below :-

$data_checkbox1 = array(
'name' => 'qualification[]',
'value' => 'Graduation'
);

$data_checkbox2 = array(
'name' => 'qualification[]',
'value' => 'Post Graduation'
);

echo form_checkbox($data_checkbox1);
echo form_checkbox($data_checkbox2);

The above code will produce the following HTML tag :-

<input type="checkbox" name="qualification[]" value="Graduation" />
<input type="checkbox" name="qualification[]" value="Post Graduation" />

 

Form Textarea

form_textarea( ) function is used to create a textarea field in CodeIgniter. To define the height and width of textarea we have to pass an associative array with index ‘rows’ and ‘cols’ with numeric value.

Example of form_textarea( ) function is given below :-

$data_textarea = array(
'name' => 'textarea',
'rows' => 10,
'cols' => 50
);
echo form_textarea($data_textarea);

The above code will produce the following HTML tag :-

<textarea name="textarea" cols="50" rows="10" ></textarea>

 

From Fieldset

form_fieldset( ) function is used to create a fieldset in CodeIgniter. Remember to use form_fieldset_close( ) function to close fielset tag.

Example of form_fieldset( ) function is given below :-

$data_fieldset = array(
'id' => 'employee_info',
'class' => 'employee_detail'
);
echo form_fieldset('Personal Detail', $data_fieldset);
echo "<p>Write your details here</p>";
echo form_fieldset_close();

The above code will produce the following HTML tag :-

<fieldset id="employee_info" class="employee_detail">
<legend>Personal Detail</legend>
<p>Write your details here</p>
</fieldset>

 

File Upload

form_upload( ) function is used to create a upload field. For this, we have to only pass an associative array with index ‘type’ and value ‘file’. Other functionality is same as form_input() function.

Example of form_upload( ) function is given below :-

$data_upload = array(
'type' => 'file',
'name' => 'file_upload'
);
echo form_upload($data_upload);

The above code will produce the following HTML tag :-

<input type="file" name="file_upload" value="" />

 

Form Submit

form_submit( ) function is used to create a submit button in CodeIgniter.

Example of form_submit( ) function is given below :-

<?php echo form_submit('submit', 'Submit'); ?>

The above code will produce the following HTML tag :-

<input type="submit" name="submit" value="Submit"  />

 

Form Reset

form_reset( ) function is used to create a reset button in CodeIgniter.

Example of form_reset( ) function is given below :-

<?php echo form_reset('reset', 'Reset'); ?>

The above code will produce the following HTML tag :

<input type="reset" name="reset" value="Reset" />

 

Form Close

form_close( ) function is used to create closing form tag.

Example of form_close( ) function is given below :-

<?php echo form_close( ); ?>

The above code will produce the following HTML tag:

</form>

Let’s have a look at some other functions which are used to set values in different tags.

  • set_value( ) function helps to set  default value for a input text field. The first parameter should be a name of an input field and the second parameter will be a value you want as a default value.

Example :-

<input type="text" name="country" value="<?php echo set_value('country', 'India'); ?>"  />
  • set_select( ) function help you to create a select field and also allow you to set a default value for the select field. The first parameter contains the name of the select field, the second parameter contains values for each item and third parameter allow us to set default item.

Example :-

 <select name="experience">
<option value="fresher" <?php echo set_select('experience', 'fresher', TRUE); ?> >fresher</option>
<option value="1-5 years" <?php echo set_select('experience', '1-5 years'); ?> >1-5 years</option>
<option value="above 5 years" <?php echo set_select('experience', 'above 5 years'); ?> >above 5 years</option>
</select>
  • set_checkbox( ) function help you to create a checkbox and also allowed you to set a default value. The first parameter contains the name of the checkbox, second parameter contain its values and third parameter allow us to set a default value.

Example :-

<input type="checkbox" name="Graduate" value="Graduate" <?php echo set_checkbox('quralification', 'Graduate', TRUE); ?> />
<input type="checkbox" name="Post_Graduate" value="Post_Graduate" <?php echo set_checkbox('qualification', 'Post_Graduate'); ?> />
  • set_radio( ) function help you to create radio buttons and also allowed you to set a default value. The first parameter contains the name of a radio button, second parameter contain its values and third parameter allow us to set a default value.

Example :-

<input type="radio" name="gender" value="Male" <?php echo set_radio('gender', 'Male', TRUE); ?> />
<input type="radio" name="gender" value="Female" <?php echo set_radio('gender', 'Female'); ?> />

Now, let’s use the above-mentioned functions and create a form which will help you to understand the functionality of each function more clearly.

 

Before we start coding, we have to make following changes in the configuration files of CodeIgniter.

  • Open ‘application/config/config.php file’ and set base URL as given below :-
    $config['base_url'] = 'http://localhost/form_helper/';
  • Now  open ‘application/config/autoload.php’ file and load form helper library as given below
    $autoload['helper'] = array('url','file','form');

Now make a file name form.php in ‘application/controller’ folder of CodeIgniter and write the codes given below inside form.php:-

 


The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.