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.
Codeigniter Form Fieldset, Text Area, Password, Checkbox Tutorial for Beginners
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.
<fieldset class=”myfieldset” id=”myfieldsetid”>
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.
$textarea=array( 'class'=>'mytextarea', 'id'=>'mytextarea-1', 'rows'=>'5', 'cols'=>'50' ); echo form_textarea($textarea);
<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.
$password_array=array( ‘name’=>’password’, ‘id’=>’password’, ‘value’=>’’, ‘placeholder’=>’enter your password’ );
<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.
The checkbox can be used in either ways:
echo form_checkbox('checkbox-1', '1', TRUE);
<input type="checkbox" name="checkbox-1" value="1" checked="checked"/>
$checkbox2=array( 'class'=>'checkbox', 'id'=>'checkbox-2', 'name'=>'checkbox-2', 'value'=>'hello', 'checked'=>'checked' ); echo form_checkbox($checkbox2);
<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.
Codeigniter Form Tutorial for Beginners Part 3
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.