Codeigniter tutorial

Codeigniter Form Tutorial for Beginners Part 4

Views: 446

In our previous tutorial we learned about some of the important form functions of codeigniter- radio buttons, select, multi select, submit and reset functions to generate appropriate elements, in this codeigniter’s form tutorial part we will learn about the other important form functions of codeigniter like creating hidden fields, button, file upload button, form error messages, form validation errors.

If you have missed the previous tutorial then you can follow the following link to read that

Codeigniter Form Tutorial for Beginners Part 3

Let us begin.

Codeigniter- Creating Hidden Fields, Buttons, File Upload Button, Form Error Messages, Form Validation Errors For Beginners

Tutorial for Creating Hidden Fields with Codeigniter’s “form_hidden()” function

In many occasions we need to have a hidden field for processing our forms and for the values we don’t want to display to the end user which gives a good user experience for our forms, in such cases we need to use a hidden field sandwiched in our forms and make them hold certain values, codeigniter’s form_hidden() function allows us to add a hidden field to our forms, the form_hidden() function accepts two parameters.

  • Data: Can be an array or string. Multiple hidden fields can be created by passing array in this with key as name of field and the value as value of this field.
  • Value: This sets the value for hidden field and is string type.

Usage:

1. Single hidden field with name and value

echo form_hidden('my-hidden', 'hey this is my hidden field');
HTML Mark-up:
 <input type="hidden" name="my-hidden" value="hey this is my hidden field" />

2. Creating multiple hidden fields with associative array

$hidden = array(

'name'=>'my-name',

'email'=>'myemail@demo.com'

);

echo form_hidden($hidden);

HTML Mark-up:

<input type="hidden" name="name" value="my-name" />

<input type="hidden" name="email" value="myemail@demo.com" />

Tutorial for Creating Buttons with Codeigniter’s “form_button()” function

We need many operations to be performed while a form is filled up by users, and in many cases we need to add a button to our UI for adding more functionalities, codeigniter’s form_button() function allows us to let add a button in our form or anywhere in our webpage with ease. The codeigniter’s form_button() function accepts three parameters.

  • Data: Can be an array or string, the attributes for the button are passed with this.
  • Content: Content is the button’s text that will be displayed to user and is a string type.
  • Extra: Extra allows additional attributes added to the button.

Usage:

$button=array(

'class'=>'btn btn-default',

'id'=>'my-button',

'name'=>'my-button'

);

echo form_button($button, 'My Button');

HTML Mark-up:

<button name="my-button" type="button" class="btn btn-default" id="my-button" >My Button</button>

Tutorial for Creating Upload Buttons with Codeigniter’s “form_upload()” function

When we need our users to submit soft copies of any document, image or files we need them to be uploaded in our server, the process of uploading files start from a basic upload button, without an upload button in our application users will fail to upload any file from their computer or media sources, codeigniter’s form_upload() function allows us to add a upload button. The form_upload() accepts three parameters.

  • Data: Can be an array or string, the attributes for the upload button are passed with this.
  • Value: Value is the upload button’s value.
  • Extra: Extra allows additional attributes added to the upload button.

Usage:

$upload=array(

'class'=>'btn btn-info',

'id'=>'my-upload-button',

'name'=>'my-upload-button'

);

echo form_upload($upload, 'Selected Files');

HTML Mark-up:

<input type="file" name="my-upload-button" class="btn btn-info" id="my-upload-button"/>

Tutorial for Displaying Field Errors with Codeigniter’s “form_error()” function

When user does some mistake and misses a mandatory field while filling out our forms, we need to notify user and to display an error message, codeigniter’s form_error() function allows us to display a field error message returned from the codeigniter’s form  validation library with ease, the codeigniter’s form_error() function allows three parameters.

  • Field: The “name” of the field for which this error message is.
  • Prefix: The content that will be prepended to the error message. i.e. A div or a span.
  • Suffix: The content that will be appended to the error message.

Usage:

Let’s assume we want to display our error message for the field with name “username” and sandwich it with a span with class “error_message” then we will use the following syntax:

Usage:

echo form_error('username', '<span class="error_message">', '</span>');

HTML Mark-up:

<span class="error_message">The returned error message from codeigniter's form validation library</span>

Tutorial for Displaying Form Errors with Codeigniter’s “validation_errors()” function

When user types in some wrong information, we cannot accept that wrong information, so we need to validate our fields and display what information is wrong to that user, codeigniter’s validation_errors() function allows us to dump all the validation error messages generated from the codeigniter’s form validation library on the user’s screen. The validation_errors() accepts two parameters.

  • Prefix: The content that will be prepended to the error message i.e. A div or a span.
  • Suffix: The content that will be appended to the error message.

Usage:

Suppose user entered a wrong username and we want to display a validation error message wrapped between a span of class validation_error then we will use the following syntax.

Note: Before you display validation error messages, make sure you have loaded the validation library.

Syntax for loading validation library: $this->load->library(‘form_validation’);

echo validation_errors('<span class="validation_error">', '</span>');

HTML Mark-up:

The validation error messages will be generated for each field separately. Like following sample.

<span class="validation_error">The validation error message generated by codeigniter's validation library will be displayed here</span>

Codeigniter Form Radio, Select, Multi Select, Submit and Reset Tutorial for Beginners

Codeigniter Form Tutorial for Beginners Part 4 Sample Files

In next tutorial we will learn how we can submit a form and process it, the sample files for this tutorial are attached below, if you want to review that please download and view. If you have any suggestion or question then please comment below, if you found this part of tutorial helpful then like our page and share with friends.

Comments: 1

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