CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities allow the user to interact and submit their data in a form.

Loading the Form Helper

Before using the Form helper, you must load the helper into the controller's files by the following syntax.

Syntax

$this->load->helper (‘form’);

Functions of Form Helper

1.form_open(): It is a function that is used to open the form tag in CodeIgniter.

Syntax

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

It has three parameters as follows

  • $action: It defines the form action/target URL.
  • $attributes: The attributes contain the values in the form of associative array.
  • $hidden: It contains all the hidden values in the form of associative array.

Example

//It is used for transferring control at define parameters.
 echo form_open( 'Online/form' ); 
 /*it define additional feature that call at initialising of the form. */
 $attributes = array('class' => 'online', 'id' => 'form'); 
 echo form_open('Online/form', $attributes);
 // It will hide the details which you have defined in $hidden.
 $hidden = array( 
     'type'  => 'text',
     'name'  => 'Maeve',
     'value' => '[email protected]',
     'class' => 'onlinehub '
 );
 echo form_open('Online/form', $attributes, $hidden);  

Note: The $attributes and $hidden parameters are optional.

2. form_close(): A form_close() function is used to close the open form tag that was opened using the form_open() function.

Syntax

echo form_close();

Example

echo form_open( 'Online/form' ); 
 /* It creates a body of the form, in which you can define multiple fields such as username, password, gender, etc. */
 echo form_close(); 

3. form_open_multipart(): It is slight different from the form_open() function. It allows the user to upload any type of files in the form.

Syntax

form_open_multipart ( [$action = ‘ ‘ [,
$attributes =array() [, $hidden = array() ] ] ] );

It has three parameters:

  • $action: It defines the form action/target URL.
  • $attributes: It uses a multipart attribute for uploading multi file in the browser.
  • $hidden: It contains all the hidden values in the form of an associative array.

Example

echo form_open_multipart( 'Online/form', $attributes);
 echo form_close(); 

4. form_hidden(): It is used to hide the input fields such as name, value, URL, etc. It is also used as an associative array to hide the multiple fields.

Syntax:

form_hidden( $name [, $value = ‘ ‘ ]);
  • $name: it is used to define the field name.
  • $value: It is used to take the field value.

Example

echo form_open();
 echo form_hide( ‘student’ , ‘Otis’);
 echo form_close(); 

5. form_input(): It is used to create text fields for the user. In this box, the user can insert the values of the field. It can also be used as an associative array to store or represent any type of data in a form.

Syntax

form_input ([ $data = ‘ ‘ [, $value = ‘ ‘
[, $extra = ‘ ‘] ] ]);

It has three parameters:

  • $data: It is the fields’ attribute name.
  • $value: It is used as the values of the fields’ names.
  • $extra: If you want to add more functionalities in your login form, you can define this parameter.

Example:

$input_data = array(
 'name' => 'tname',
 'id' => 'tname_id',
 'value' => 'ERIC '
 'readonly' => 'readonly'
 'placeholder' => 'Enter Your Name'
 ); 
 echo form_input($input_data); 

6. form_password(): As its name suggests, this function is used to create a text box for setting the passwords in the forms.

Syntax

form_password ( [ $data = ‘ ‘[, $value = ‘
‘[ , $extra = ‘ ‘] ] ]);

It has three parameters:

  • $data: It is used to provide the fields name.
  • $value: It is used as the values of the field’s name.
  • $extra: If you want to add more functionalities in your login form, you can define it in this parameter.

Example:

$input_data = array(
 'name' => 'tname',
 'id' => 'tname_id',
 'value' => 'ERIC '
 'readonly' => 'readonly'
 'placeholder' => 'Enter Your Name'
 ); 
 echo form_input($input_data); 

7. form_upload(): This function is used to upload the documents in the form such as image, audio, video, pdf files, etc.

Syntax

form_upload( [ $data = ‘ ‘[, $value = ‘ ‘
[, $extra = ‘ ‘] ] ]);

It has three parameters:

  • $data: It is the fields’ name.
  • $value: It is the value of the fields’ names.
  • $extra: It is used to add additional functionality in the form_upload function.

Example:

$upload_data = array(
 'type' => 'file',
 'name' => 'upload_file'
 );
 echo form_upload($upload_data); 

8. form_textarea(): A form_textarea() function is used to store large information such as addresses and messages in the form.

Syntax

form_textarea ( [ $data = ’ ‘ [, $value = ‘
‘[, $extra = ‘ ‘] ] ]);

It has three parameters

  • $data: It is the fields’ name.
  • $value: It is the value of the fields’ name.
  • $extra: It is used to add additional functionality in the form_textarea() such as placeholder, size, etc.

Example:

$rough_textarea = array(
 'name' => 'textarea',
 'rows' => 5,
 'cols' => 30
 );
 echo form_textarea($rough_textarea); 

9. form_dropdown():  A form_dropdown() function is used to provide a list of options where a user can select a option.

Syntax

form_dropdown ([ $name = ‘ ‘ [, $options =
array() [, $selected = array() [, $extra = ‘ ‘ ] ] ] ]);

It has four parameters:

  • $name: It denotes the field name of the dropdown.
  • $options: It is used to store multiple values in array format.
  • $selected: It contains a list of the selected field values taken from the list of fields in an array format.
  • $extra: It is used to perform additional functionalities in the dropdown.

Example:

  form_dropdown ([ $name = ‘ ‘ [, $options = array() [, $selected = array() [, $extra = ‘ ‘ ] ] ] ]); 

10. form_multiselect(): It is similar to form_dropdown() function, but it has small changes such as it allows the user to select multiple options from the options given in the list.

Syntax:

form_multiselect ([ $name = ‘ ‘ [, $options
= array() [, $selected = array() [, $extra = ‘ ‘ ] ] ] ]);

It has four parameters:

  • $name: It denotes the field name of the dropdown.
  • $options: It is used to store multiple values in array format.
  • $selected: It contains a list of the selected field values from the list of fields in an array format.
  • $extra: It is used to define the additional functionalities that you want to perform in the multiselect options.

Example

 echo form_label('Languages Known '. " ");
  $language = array(
    'AP' => 'APPLE',
    'BN' => "BANANA",
    'MN' => 'MANGO',
    'OR' => 'ORANGE',
    'PA' => 'PINE APPLE',
    
  );
  
  echo " ".form_multiselect("Show[]", $language); 

11.form_fieldset(): This function is used to represents group related information in a form.

Syntax

form_fieldset ( [ $legend_text = ‘ ‘ [,
$attributes = array() ] ]);

It has two parameters:

  • $legend_text: It is used to define a tile or the caption in the form_fieldset().
  • $attributes: This defines the attributes to be set in form_fieldset ().

Example:

echo form_open()
 $fieldset_data = array(
 'id' => 'student_info,
 'class' => 'student_detail'
 ); 
 echo form_fieldset(‘Student information, $fieldset_data);
 echo "<p>Enter your details here</p>";
 echo form_close(); 

12. form_fieldset_close(): This function is used to append some extra attributes after the closing of the form_fieldset().

Syntax

form_fieldset_close ( [ $extra = ‘ ‘ ]);
  • $extra: It is used to append any information after the closing tag.

Example:

$fieldset_data = array(
 'id' => 'student_info,
 'class' => 'student_detail',
 'name'  => 'Maeve'
 );
 echo form_fieldset(‘Student information, $fieldset_data); 
 echo "<p>Enter your details here</p>";
 echo form_fieldset_close(); 

13.form_checkbox(): The form_checkbox() function is used to check one or more options from a limited number of choices.

Syntax

form_checkbox ( [ $data = ‘ ‘ [, $value = ’
‘ [, $checked = ‘ ‘ [, $extra = ‘ ‘] ] ] ]); 

It has four parameters:

  • $data: It represents the field name of the checkbox.
  • $value: It is used as field value in the checkbox.
  • $checked: It shows the default checked items from the given lists.
  • $extra: It is used to shows the extra attributes to be added in the checkbox such as array or string.

14. form_radio(): The form_radio() function is similar to form_checkbox(), but it has small changes such as it uses radio option to select more than one item in the given lists.

Syntax

form_radio ([ $data = ‘ ‘ [, $value = ‘ ‘
[, $checked = FALSE [, $extra = ‘ ‘ ] ] ] ] );

It has four parameters:

  • $data: It is used as field name of the form_radio().
  • $value: It is used as field value in the radio.
  • $checked: It shows the default selected items from the given lists. It uses a Boolean value to show pre-checked data items in the list.
  • $extra: It is used to show the extra attributes to be added in the form_radio such as array or string.

Example

$radio_data1 = array(
 'name' => 'gender',
 'value' => 'male',
 'checked' => TRUE,
 );
 $radio_data2 = array( 
 'name' => ' gender,
 'value' => 'female',
 ); 
 echo form_radio($radio_data1);
 echo form_radio($radio_data2); 

15. form_label(): The for_label() function create a relationship with an input element in the form field.

Syntax

form_label ( [ $label_text = ‘ ‘[, $id = ‘
‘ [, $attributes = array() ] ] ] );

It has three parameters:

  • $label_text: It defines the text that is placed in between of the <label> tag.
  • $id: It defines the ID of the form element that can be called in the label tag.
  • $attributes: It defines the HTML attributes.

Example

echo form_label('Password :');

16. form_submit(): It is used to create a submit button that allows the user to submit their details in the form.

Syntax

form_submit ( [ $data = ‘ ‘ [, $value = ‘ ‘
[, $extra = ‘ ‘ ] ] ] );

It has three parameters:

  • $data: It defines the button name.
  • $value: It defines the value of the button.
  • $extra: It is used to shows the extra attributes to be added in the submit botton such as array or string.

Example:

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

17. form_reset(): The form_reset () function is used to create a reset button that allows users to reset all the values entered in a form.

Syntax

form_reset ( [ $data = ‘ ‘ [, $value = ‘ ‘
[, $extra = ‘ ‘ ] ] ] );

It has three parameters:

  • $data: It defines the button name.
  • $value: It shows the value of the button, which you have defined in the reset button.
  • $extra: It is used to shows the extra attributes to be added in the reset button, such as array or string.

Example

<?php echo form_reset('edit', 'EDIT'); ?>

18. form_button(): This function is used to create a button in the form. But it has small change as compared to form_submit and the form_reset button such as it takes the content of the button label instead of the button value.

Syntax

form_button ( [ $data = ‘ ‘ [, $content = ‘
‘ [, $extra = ‘ ‘ ] ] ] );

It has three parameters:

  • $data: It defines the button name.
  • $content: It shows the content of the button label instead of button value.
  • $extra: It is used to shows the extra attributes to be added in the form button either as an array or string.

19. set_value(): This function is used to set the default value of an input or textarea in the form.

Syntax

set_value ( $field [, $default = ‘ ‘ [,
$html_escape = TRUE ] ]);

It has three parameters:

  • $field: It show the field name.
  • $default: It is optional parameters that allow setting a default value in the form.
  • $hml_escape: It is an optional parameter that permits to turn off HTML escaping the value.

20. form_error(): The form_error() function is used to show an error message in the specified field name of a form.

Syntax

form_error ( [ $field = ‘ ‘ [, $prefix = ‘
‘ [, $suffix = ‘ ‘ ] ] ]);

It has three parameters:

  • $field: It shows a field name in the form.
  • $prefix: It is the error opening tag in the form.
  • $suffix: It is used to show error in the closing tag of the form_error().

21. validation_errors(): This function is used to show an error message in the forms when the validation rule is not satisfied in the form fields.

Syntax

validation_errors ( [ $prefix = ‘ ‘ [,
$suffix = ‘ ‘] ] );

It has two parameters:

  • $prefix: It define all the error opening tag.
  • $suffix: It defines all the error closing tags.

22. form_prep(): This function is used in the form helper to safe use of html tag and character such as use of quotes within the form element without breaking out of the syntax formation.

Syntax

form_prep ( $str );
  • $str: It defines the value to be escaped from the form.

Create a registration form in CodeIgniter

Create a controller file Online.php and save it in application/controller folder. After that, write the following program in your controller file.

Online.php

<?php
 class Online extends CI_Controller
 {
     public function form_helper() {
         $this->load->helper('form');
         $this->load->view('Online/form');    
     }
 }
 ?> 

Create a views file Form.php and save it in application/views/Online/form.php. After that, write the following program in your views file.

Form.php

<?php
echo "<title>Tutorial and Example </title>";
 // form_open() function
 echo form_open();
 // form_fieldset() function
 echo form_fieldset('Student Registration form'); 
 echo "<h2> <center> Enter your Details </center> </h2>";
 echo "<hr></hr>";
 $data = array(
     'name'          => 'uname',
     'id'            => 'uname',
     'value'         => '',
     'maxlength'     => '100', 
     'size'          => '50',
     'style'         => 'width:50%',
     'placeholder' => 'ENTER YOUR NAME',
     'title' => 'Please Provide your name',
 );
 echo "NAME :  ". "   ".form_input($data). "<br> <br>";
 //echo "PASSWORD :". form_password
 echo form_label('Password :'); 
  $password = array('name'=>'passowrd', 'id'=>'password', 'style' => 'width:50%', 'maxlength'=>'11', 
  'placeholder'=> 'ENTER YOUR PASSWORD');
 // form_password() function
  echo form_password($password). "<br> <br>";
  echo form_label('Email :');
  echo form_input(array('id' => 'email', 'type' => 'email', 'name' => 'email', 'value'=>'',
  'style' => 'width:50%', 'border' => '1',
 'placeholder' => 'ENTER YOUR EMAIL-ID')); 
 echo "<br><br>";
 echo form_label('Address :'); 
 // form_textarea() function 

  echo form_label('Php :');
  $data = array('name'=>'sunject', 'id'=>'php', 'value'=>'accept',
 'checked'=>false, 'style'=>'margin:20px');
  echo form_checkbox($data);
  echo form_label('java :'); 
  $data = array('name'=>'subject', 'id'=>'Java', 'value'=>'java',
 'checked'=> TRUE, 'style'=>'margin:20px');
  echo form_checkbox($data);
  echo form_label('.Net :');
  $data = array('name'=>'subject', 'id'=>'.Net', 'value'=>'dotNET', 
 'checked'=> false, 'style'=>'margin:20px');
  echo " ".form_checkbox($data);
  echo form_label('C Programming :');
  $data = array('name'=>'subject', 'id'=>'C', 'value'=>'C',
  'checked'=> false, 'style'=>'margin:20px');
 // form_checkbox() function
   echo " ".form_checkbox($data); 
   echo form_label('CODEIGNITER :');
   $data = array('name'=>'subject', 'id'=>'CODEIGNITER', 'value'=>'code',
   'checked'=> TRUE, 'style'=>'margin:20px');
    echo " ". form_checkbox($data). "<br>";
 echo form_label('Gender :');
 $data = array(
   'name'          => 'gender',
   'id'            => 'male', 
   'value'         => 'M',
  // 'checked'       => TRUE,
   'style'         => 'margin:10px'
 ); 
 $data2 = array(
   'name'          => 'gender',
   'id'            => 'female',
   'value'         => 'F',
  // 'checked'       => TRUE,
  'style'         => 'margin:10px'
 );
 // form_radio() function
  echo form_label( form_radio($data)."Male");
  echo form_label(form_radio($data2). "Female"."<br>" );
  // form_label() and form_input() function
  echo form_label('Mobile No.: ');
  echo form_input(array('id' => 'mobile', 'name' => 'mobile',  'maxlength' => '10'))."<br></br>"; 
 // form_dropdown() function
 echo form_label('Country');
  $cname = array(
    'USA' => 'UNITED STATE AMERICA',
    'IN' => "INDIA", 
    'AUS' => 'AUSTRALIA',
    'UK' => 'UNITED KINGDOM',
    'NP' => 'NEPAL'
  );
  echo form_dropdown("Show", $cname);
  echo "<br> <br>"; 
 // form_multiselect() function
  echo form_label('Select Multiple Languages '. " ");
 $language = array(
    'eng' => 'ENGLISH', 
    'hn' => "HINDI",
    'jp' => 'JAPANESE',
    'cn' => 'CHINESE',
    'NP' => 'FRENCH',
    'sp' => 'SPANISH'
  );
  //$selected = array ('IN', 'USA');
  echo " ".form_multiselect("Show[]", $language);
  echo "<br> <br>";
 // form_upload() function
 echo form_label("Upload file:");
 $upload_file = array(
 'type' => 'file', 
  'name' => 'upload_file'
  );
   echo form_upload($upload_file)."<br> <br>";
  // form_submit() function
  echo form_submit('Sub', 'SAVE');
 // form_reset() function 
   $data = array('name'=>'resetbutton', 'id'=>'button', 'value'=>'RESET',
 'content'=>'Reset');
  echo form_reset($data);
  echo form_fieldset_close();
 echo form_close();
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/online/, it displays a form on the screen, as shown below.

CodeIgniter Form Helper