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

Functions of Form Helper

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

Syntax

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

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

Example

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

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

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:

  • $name: it is used to define the field name.
  • $value: It is used to take the field value.

Example

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

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:

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

Syntax

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:

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

Syntax

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:

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

Syntax

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:

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

Syntax

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:

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:

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

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

Syntax

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:

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

Syntax

  • $extra: It is used to append any information after the closing tag.

Example:

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

Syntax

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

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

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

Syntax

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

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

Syntax

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:

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

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

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

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

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

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

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

  • $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

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

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

Pin It on Pinterest

Share This