Codeigniter tutorial

Codeigniter Form Tutorial for Beginners Part 2

Views: 567

In previous tutorial we learned about codeigniter’s form helper and loading the form helper, with that we learned about the form_input() function and learned about adding various parameters to them, in this tutorial we will learn about the codeigniter functions to generate fieldset, text area, password input and checkboxes for our forms. Link for previous tutorial is below.

Let’s start

Codeigniter Form Fieldset, Text Area, Password, Checkbox Tutorial for Beginners

tutorial-for-creating-fieldset-and-legends-with-codeigniter-form_fieldset

Tutorial for Creating Fieldset and Legends with Codeigniter “form_fieldset()”

A fieldset is used to group the form elements and legend is used to name that group, when we are creating a form with different groups in it then fieldset and legend come handy to use. The fieldset in codeigniter can be generated by echoing form_fieldset() function and the fieldset element is closed using the form_fieldset_close() function, let us see the demonstration below about form_fieldset() usage.

Accepted form_fieldset() parameters

  • Legend: Text for the legend.
  • Attributes: Is an array and contains all attribute and its values.

 

Usage:

$fieldset=array(‘class’=>’myfieldset’,

‘id’=>’myfieldsetid’);

echo form_fieldset(‘Login’,$fieldset);

echo form_fieldset_close();

HTML Mark-up:

<fieldset class=”myfieldset” id=”myfieldsetid”>

<legend>Login</legend>

</fieldset>

 

Tutorial for Creating Text Area With Codeigniter “form_textarea()”

A textarea is used when we want to take a multi line or large text input from user, in many cases we need it to be added in our forms, codeigniter’s form_textarea() function allows us to add text areas in our forms. The form_textarea() function supports three parameters

  • Data: It is an array of HTML attributes for text area.
  • Value: This is the value of text area and is a string.
  • Extra: This is for other extra HTML attributes we add to our text area. It can be either array or string.

Usage:

$textarea=array(

'class'=>'mytextarea',

'id'=>'mytextarea-1',

'rows'=>'5',

'cols'=>'50'

);

echo form_textarea($textarea);

HTML Mark-up:

<textarea cols="50" rows="5" class="mytextarea" id="mytextarea-1" ></textarea>

 

Tutorial for Creating Text Area With Codeigniter “form_password()”

When it comes to security we never want to reveal our passwords, and the input fields displays all text typed in it, it is not acceptable that when you are typing your password the person behind it is able to read it, hence for these concerns we use password fields, in previous tutorial we saw usage of text inputs, now let us learn using password inputs in codeigniter with codeigniter’s form_password() function. The codeigniter’s form_password() function allows us to create password fields for our form and it also accepts three parameters

  • Data: It is an array of HTML attributes for password input.
  • Value: This is the value of password input and is a string.
  • Extra: This is for other extra HTML attributes we add to our password input. It can be either array or string.

Usage:

$password_array=array(

‘name’=>’password’,

‘id’=>’password’,

‘value’=>’’,

‘placeholder’=>’enter your password’

);

echo form_password($password_array);

HTML mark-up:

<input type=”password” name=”password” value=”” id=”password” placeholder=”enter your password”/>

Tutorial for Creating Checkbox With Codeigniter “form_checkbox()”

Checkboxes are another important asset of our forms, we use checkboxes in many occasions on our forms, the most heard phrase and common phrase where we use checkboxes is “Remember Password”, codeigniter’s form_checkbox() function allows us to add a checkbox in our form, the form_checkbox() accepts four parameters

  • Data: This is the html attributes for checkbox and is an array.
  • Value: This defines the value of checkbox and it is string.
  • Checked: This is a Boolean value which can either be true or false, true makes the checkbox checked and false keeps it unchecked.
  • Extra: This contains other HTML attributes for the checkbox, can be string or array.

Usage:

The checkbox can be used in either ways:

1.

echo form_checkbox('checkbox-1', '1', TRUE);

HTML Mark-up

<input type="checkbox" name="checkbox-1" value="1" checked="checked"/>

2.

$checkbox2=array(

'class'=>'checkbox',

'id'=>'checkbox-2',

'name'=>'checkbox-2',

'value'=>'hello',

'checked'=>'checked'

);

echo form_checkbox($checkbox2);

HTML mark-up

<input type="checkbox" name="checkbox-2" value="hello" checked="checked" class="checkbox" id="checkbox-2"/>

That’s all for this part of tutorial, in next tutorial we will learn about codeigniter’s select, multi select, radio button, form submit and reset functions. The sample view file for this part of tutorial is attached below, if you want to take a look download the file and go through.

sample-view-file

Codeigniter Form Tutorial for Beginners Part 3

sample output codeigniter form

 

If you found this part of tutorial helpful, please comment below and share with your friends, and also if you have any suggestions or questions write to me in the comments below.

Comments: 2

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