Codeigniter tutorial

Codeigniter Form Tutorial for Beginners Part 1

Views: 830

A form is the most important view part for any web application, without having a form you cannot take user inputs and hence your users will not able to interact with your application properly, hence knowing about forms and knowing proper implementation of a form is very important for an application developer in this tutorial part we will learn about various codeigniter form functions and the codeigniter form helper. Let’s start

Codeigniter Form Tutorial form_open, form_close, form_input

Codeigniter Form Tutorial Main

Loading the Codeigniter Form Helper

The codeigniter form helper contains set of functions needed for working with forms, it is important that you load the form helper before calling any form methods in codeigniter. In codeigniter we use the following syntax to load the form helper.

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

Creating a Codeigniter Form with “form_open()”

The codeigniter allows a specific function to open our new form tag with various attributes. The codeigniter form_open() function allows us to add a form opening tag with various parameters. i.e. action, HTML attributes like class, id, method and hidden fields. By default form method is ‘post’ and charset is ‘utf-8’.

Available Parameters:

The codeigniter form_open() function allows three parameters.

  1. Action: The action of form, i.e. when form is submitted which URL will be used to send form data.
  2. Attributes: This is an array set of all attributes of the form, i.e. class, id and other html attributes.
  3. Hidden: The hidden parameter is set of your hidden fields, this is also an array.

Usage:

Our View File:

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

$action='user/login';

$attributes=array('class'=>'my_form', 'id'=>'my_form' );

//$attributes contains all HTML attributes we need for the form, the attribute name is key and attribute value is the value

$hidden=array('login'=>'1', 'user_type'=>'admin');

//$hidden is the array of hidden fields with their name as key and value as the value.

echo form_open($action, $attributes, $hidden);

echo form_close();

Output:

<form action="http://localhost/testing/index.php/user/login" class="my_form" id="my_form" method="get" accept-charset="utf-8">

<input type="hidden" name="login" value="1" style="display:none;" />

<input type="hidden" name="user_type" value="admin" style="display:none;" />

</form>

 

Creating Codeigniter Forms with multipart/form-data “form_open_multipart()”

Multipart/form-data encryption is a very important attribute of html forms, whenever we need to send some file upload request we need to use multipart/form-data encryption, codeigniter allows multipart/form-data encryption with the form_open_multipart() function. This also accepts three parameters as codeigniter’s form_open().

Usage:

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

$action='user/login';

$attributes=array('class'=>'my_form', 'id'=>'my_form' );

$hidden=array('login'=>'1', 'user_type'=>'admin');

//$hidden is the array of hidden fields with their name as key and value as the value.

echo form_open_multipart($action, $attributes, $hidden);

echo form_close();

Output:

<form action="http://localhost/testing/index.php/user/login" class="my_form" id="my_form" enctype="multipart/form-data" method="post" accept-charset="utf-8">

<input type="hidden" name="login" value="1" style="display:none;" />

<input type="hidden" name="user_type" value="admin" style="display:none;" />

</form>

Codeigniter Closing Form Tag with form_close()

The form_close() function allows to close our form tag and completes the form, once you are done building the form as per needs echo the form_close() function, this will close the form.

Codeigniter Adding Form Labels with form_label()

If you have created forms in HTML then you are already aware of the labels, this is what we used to create textual information about our form fields, the usage of form_label() is very simple and straight forward.

Usage:

echo form_label('Enter your name:');

Output:

<label>Enter your name:</label>

The label_for() function supports three attributes

  1. Label Text: This is the text that will be printed on user’s screen.
  2. Label For value: This is the value of for attribute.
  3. HTML attributes: This is the array of html attributes for our label field.

See the example below:

Usage:

echo form_label('Enter your name:','name',array('class'=>'my_label', 'id'=>'my_name_label'));

Output:

<label for="name" class="my_label" id="my_name_label">Enter your name:</label>

Adding some input elements to our codeigniter form with “form_input()”

The codeigniter form_input() function allows us to add input fields to our form, it can accept 3 parameters and apply to our input element accordingly.

  1. Data: This is array of HTML attributes needed for input field.
  2. Value: This contains string value for the input field.
  3. Extra: These are the extra parameters added to the input element, it can be array or string.

A simple codeigniter input element with “form_input()”

We can produce a simple input element with its name and value parameters just by passing the first parameter as name and second parameter as its value to the form_input() function.

Usage:

echo form_input('name', 'Bill Gates');

Output:

<input type="text" name="name" value="Bill Gates"  />

Codeigniter’s Input element with array “form_input()”

It is also possible that we pass all the attributes as an array in the first parameter of form_input() to create a extended input element with our additional attributes.

Usage:

$input_array=array(

'name'=>'name',

'id'=>'input-1',

'value'=>'',

'placeholder'=>'enter your name'

);

echo form_input($input_array);

Output:

<input type="text" name="name" value="" id="input-1" placeholder="enter your name"  />
Codeigniter Form Output

Here I am finishing this part of tutorial, you can download the sample view file I used for this tutorial and load it in your controller to see it in action, in next part we will learn about adding other elements like text area, textbox, fieldset, checkboxes and password inputs

Codeigniter Form Tutorial for Beginners Part 2

Sample Codeigniter form_open(), form_input(), form_close() view file

sample

If you have any suggestion or question please write in the comments below, also share with your friends if you found this tutorial part helpful.

Comments: 2

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