Tutorialsplane

Codeigniter Form Helper


Codeigniter Form Helper – Codeigniter Form helper provides various functions that are used to create the form $this->load->helper(‘form’); is used to load the helper. The Form Helper file contains functions that assist in working with forms. Here in this tutorial, we are going to explain how to create a form in CodeIgniter.


Codeigniter Form Helper Example

Now we will see how to load form helper in codeigniter and then use its function-

Load Form Helper

How To Load Form Helper in Codeingniter Example:
 
$this->load->helper('form');

Functions:-

There are many functions are available in Form helper. Now we will explain one by one with example.

1. Form Open and Adding Attributes

Syntax of form_open and Adding Attributes function is

Syntax of form_open and Adding Attributes function is:-

form_open([$action = ''[$attributes = ''[ $hidden = array()]]])
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);		

Parameters:

  • $action (string) : Form action/target URI string
  • $attributes (array) : HTML attributes
  • $hidden (array) : An array of hidden fields’ definitions
  • Returns: : An HTML form opening tag
  • Return type : string

2. Form Hidden

Syntax of form_hidden function is

Syntax of form_hidden function is:-

form_hidden($name[$value = ''])		

Parameters:

  • $name (string) : Field name
  • $value (string) : Field value
  • Return type : string

3. Form Input

Syntax of form_input function is

Syntax of form_input function is:-

form_input([$data = ''[$value = ''[ $extra = '']]])
form_password([$data = ''[$value = ''[ $extra = '']]])
form_upload([$data = ''[$value = ''[$extra = '']]])
form_textarea([$data = ''[, $value = ''[, $extra = '']]])		

Parameters:

  • $data (array) : Field attributes data
  • $value (string) : Field value
  • $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
  • Return type : string

4. Form DropDown

Syntax of form_dropdown function is

Syntax of form_dropdown function is:-

form_dropdown([$name = ''[$options = array()[$selected = array()[$extra = '']]]])		

Parameters:

  • $name (string) : Field name
  • $options (array) : An associative array of options to be listed
  • $selected (array) : List of fields to mark with the selected attribute
  • $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
  • Returns : An HTML dropdown select field tag
  • Return type : string

5. Form Fieldset

Syntax of form_fieldset function is

Syntax of form_fieldset function is:-

form_fieldset([$legend_text = ''[$attributes = array()]])
form_fieldset_close([$extra = ''])		

Parameters:

  • $legend_text (string) : Text to put in the legend tag
  • $attributes (array) : Attributes to be set on the fieldset tag
  • Returns : An HTML fieldset opening tag
  • Return type : string

6. Form Checkbox and Radio Button

Syntax of form_checkbox and radiobutton function is

Syntax of form_checkbox and radiobutton function is:-

form_checkbox([$data = ''[$value = ''[$checked = FALSE[$extra = '']]]])
form_radio([$data = ''[$value = ''[$checked = FALSE[$extra = '']]]])		

Parameters:

  • $data (array) : Field attributes data
  • $value (string) : Field value
  • $checked (bool) : Whether to mark the checkbox as being checked
  • $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
  • Returns : An HTML checkbox and Radio Button input tag
  • Return type : string

7. Form Label

Syntax of form_label function is

Syntax of form_label function is:-

form_label([$label_text = ''[, $id = ''[, $attributes = array()]]])		

Parameters:

  • $label_text (string) : Text to put in the
  • $id (string) : ID of the form element that we’re making a label for
  • $attributes (string) : HTML attributes
  • Return type : string

8. Form Submit , Reset and Button

Syntax of form_submit , Reset and button function is

Syntax of form_submit , Reset and button function is:-

form_submit([$data = ''[$value = ''[$extra = '']]])	
form_reset([$data = ''[$value = ''[$extra = '']]])
form_button([$data = ''[$content = ''[$extra = '']]])	

Parameters:

  • $data (string) : Button name
  • $value (string) : Button value
  • $extra (mixed) : Extra attributes to be added to the tag either as an array or a literal string
  • Return : An HTML input submit , Reset and button tag
  • Return type : string

9. Form Close

Syntax of form_close function is

Syntax of form_close function is:-

form_close([$extra = ''])	

Parameters:

  • $extra (string) : Anything to append after the closing tag, as is
  • Return : An HTML form closing tag
  • Return type : string

10. Form Error

Syntax of form_error function is

Syntax of form_error function is:-

form_error([$field = ''[, $prefix = ''[, $suffix = '']]])	

Parameters:

  • $field (string) : Field name
  • $prefix (string) : Error opening tag
  • $suffix (string) : Error closing tag
  • Return : HTML-formatted form validation error message(s)
  • Return type : string

11. Form Validate Error

Syntax of validation_errors function is

Syntax of validation_errors function is:-

validation_errors([$prefix = ''[, $suffix = '']])	

Parameters:

  • $prefix (string) : Error opening tag
  • $suffix (string) : Error closing tag
  • Return : HTML-formatted form validation error message(s)
  • Return type : string

EXAMPLE 1

Here is simple example to Form Open and Adding Attributes.

| Form Open and Adding Attributes in Codeigniter Example:-

controllers
public function form()
	{
		$this->load->helper('form');
		$this->load->view('form_view');	
	}
views part:-
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);

EXAMPLE 2

Here is simple example to Form Hidden.

| Form Hidden in Codeigniter Example:-

views part
$data = array(
        'name'  => 'John Doe',
        'email' => 'john@example.com',
        'url'   => 'http://example.com'
);

echo form_hidden($data);

EXAMPLE 3

Here is simple example to Form Input.

| Form Input in Codeigniter Example:-

views part
echo form_label('User Name :');
$input = array('name'=>'username','id'=>'username','value'=>'johndoe',
'maxlength'=>'100','size'=>'50','style'=>'width:50%');
echo form_input($input);

echo form_label('Password :');
$password = array('name'=>'passowrd','id'=>'password','type'=>'password',
'maxlength'=>'100','size'=>'50','style'=>'width:50%');
echo form_password($password);

echo form_label('File Upload :');
$upload = array('name'=>'upload','id'=>'upload','type'=>'file');
echo form_open_multipart($upload);

echo form_label('TextArea :');
 $text_area= array('name'=>'txt_area','id'=>'txt_area','value'=>'johndoe',
 'rows'=>'5','cols'=>'10','style'=>'width:50%');
 echo form_textarea($text_area);

EXAMPLE 4

Here is simple example to Form DropDown.

| Form DropDownin Codeigniter Example:-

views part
$options = array(
        'Cse'         => 'Computer Science Engineering',
        'Ece'           => 'Electronics engg',
        'Mech'         => 'Mechanical Engg',
        'EEE'        => 'Electrical Engg',
);
echo form_dropdown('Course', $options);

EXAMPLE 5

Here is simple example to Form Fieldset.

| Form Fieldset in Codeigniter Example:-

views part
$field_set = array(
        'id'    => 'address_info',
        'class' => 'address_info'
);

echo form_fieldset('Address Information', $field_set );
echo "<p>fieldset content here</p>\n";
echo form_fieldset_close();

EXAMPLE 6

Here is simple example to Form Checkbox and Radiobutton.

| Form Checkbox and Radiobutton in Codeigniter Example:-

views part
Check box
echo form_label('course :');
echo form_label('php :');
$data = array('name'=>'course','id'=>'php','value'=>'accept',
'checked'=>false,'style'=>'margin:10px');
echo form_checkbox($data);
echo form_label('java :')?>
$data = array('name'=>'course','id'=>'java','value'=>'accept',
'checked'=> false,'style'=>'margin:10px');
echo form_checkbox($data);

Radio Button

$male=array('id'=>'2','name'=>'gen');
$female=array('id'=>'3','name'=>'gen');
echo "

EXAMPLE 7

Here is simple example to Form Label.

| Form label in Codeigniter Example:-

views part
$attributes = array(
        'class' => 'mycustomclass',
        'style' => 'color: red;'
);
echo form_label('What is your Name', 'username', $attributes);

EXAMPLE 8

Here is simple example to Form Submit and Reset Button.

| Form Submit and Reset Button in Codeigniter Example:-

views part
$data = array('name'=>'submit','id'=>'submit','value'=>'submit',
'type'=>'submit','content'=>'submit post');
echo form_submit($data);

$data = array('name'=>'button','id'=>'button','value'=>'true',
'type'=>'reset','content'=>'Reset');
echo form_reset($data);

EXAMPLE 9

Here is simple example to Form close.

| Form Close in Codeigniter Example:-

views part
$string = '';
echo form_close($string);

EXAMPLE 10

Here is simple example to Form Error.

| Form Error in Codeigniter Example:-

views part
echo form_error('myfield', '<div class="error">', '</div>');

EXAMPLE 11

Here is simple example to Form validate Error.

| Form Validate Error in Codeigniter Example:-

views part
echo validation_errors('<span class="error">', '</span>');

Complete Example To Create Contact Form Using CodeIgniter

| Create Contact Form Using Codeigniter Example:-

Controllers part
public function form()
	{
		$this->load->helper('form');
		$this->load->view('form_view');
	}

Views part
<?php echo form_open('email/send', 'class="email" id="myform"'); ??>
<?php echo form_label('Name :'); ??>

<?php echo form_input(array('id' =?> 'name', 'name' => 'name')); ?><br/>

<?php echo form_label('Email :'); ??>

<?php echo form_input(array('id' =?> 'email', 'name' => 'email', 'value'=>'sonukr321@gmail.com')); ?><br/>

<?php echo form_label('Password :');??>

<?php $password = array('name'=?>'passowrd','id'=>'password','type'=>'password',
'maxlength'=>'11');?>

<?php echo form_password($password);??><br/>

<?php echo form_label('Mobile No. :'); ??>

<?php echo form_input(array('id' =?> 'mobile', 'name' => 'mobile')); ?><br/>

<?php echo form_label('Stream. :'); ??>

<?php echo form_dropdown('Stream',array('Cse'=?>'Computer Science Engineering','Ece'=>'Electronics engg','Mech'=>'Mechanical Engg','EEE'=>'Electrical Engg')); ?><br/>

<?php echo form_label('Course :');??>

<?php echo form_label('Php :');??>

<?php $data = array('name'=?>'course','id'=>'php','value'=>'accept',
'checked'=>false,'style'=>'margin:10px');?>

<?php echo form_checkbox($data);??>

<?php echo form_label('java :')??>

<?php $data = array('name'=?>'course','id'=>'java','value'=>'accept',
'checked'=> false,'style'=>'margin:10px');?>

<?php echo form_checkbox($data);??>

<?php echo form_label('.Net :')??>

<?php $data = array('name'=?>'course','id'=>'.Net','value'=>'accept',
'checked'=> false,'style'=>'margin:10px');?>

<?php echo form_checkbox($data);??>
<br/>

<?php $male=array('id'=?>'2','name'=>'gen');?>
<?php $female=array('id'=?>'3','name'=>'gen');?>
<?php echo "<label?>Gender : 

The output of the above example will be something like this –

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference