Codeigniter

How to create form in CodeIgniter

For working with form in CodeIginter you have to use form helper.
Load the form helper

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

You can also load the form helper in application/config/autoload.php.

$autoload['helper']=array('form');

Benefit of loading from  helper in the autoload.php is that if you have multiple form then you have to use form helper in every controller but loading form helper in autoload.php  then no need to load form helper in every controller. It will automatically autoload in every controller.
How to start a from

<?php echo from_open($action,[$attributes])?>

$action (string) : Main controller
$attributes (array) : HTML attributes like name, class etc.
For ex :

<?php echo from_open('controller/method',['name'=>'signup'])?>

This will produce

<form name="signup" method="post">

How to create input text field 

<?php echo form_input(['name'=>'fullname','id'=>'fullname','placeholder'=>'Enter your full name','class'=>'form-control']);?>

This will produce

<input type="text" name="fullnmae" id="fullname" placeholder="Enter your full name" class="form-control">

How to create password text field 

<?php echo form_password(['name'=>'password','id'=>'password','placeholder'=>'Enter your password','class'=>'form-control']);?>

This will produce

<input type="password" name="password" id="password" placeholder="Enter your password" class="form-control">

How to create drop down list

<?php echo form_dropdown('name',$options,$selectedvalues)?>

name : name of the drop down list
$options : list of the dropdowns
$selectedvalues :  If you wish to be selected a value
Ex for dropdwon list : 

<?php
$options=array(
'PHP'=>'PHP'
'MySQL'=>'MySQL'
'Wordpress'=>'Wordpress'
'HTML'=>'HTML'
);
echo form_dropdown('skill',$options);?>

This will produce like

<select name='skill'>
<option value="PHP">PHP</option>
<option value="MySQL">MySQL</option>
<option value="Wordpress">Wordpress</option>
<option value="HTML">HTML</option>
</select>

If you wish to a selected one value

<?php
$options=array(
'PHP'=>'PHP'
'MySQL'=>'MySQL'
'Wordpress'=>'Wordpress'
'HTML'=>'HTML'
);
echo form_dropdown('skill',$options,'PHP');?>

This will produce like

<select name='skill'>
<option value="PHP" selected='selected'>PHP</option>
<option value="MySQL">MySQL</option>
<option value="Wordpress">Wordpress</option>
<option value="HTML">HTML</option>
</select>

If you wish to  selected multiple value

<?php
$options=array(
'PHP'=>'PHP'
'MySQL'=>'MySQL'
'Wordpress'=>'Wordpress'
'HTML'=>'HTML'
);
$selectedskills=array('PHP','MySQL');
echo form_dropdown('skill',$options,$selectedskills);?>

This will produce like

<select name='skill'>
<option value="PHP" selected='selected'>PHP</option>
<option value="MySQL" selected='selected'>MySQL</option>
<option value="Wordpress">Wordpress</option>
<option value="HTML">HTML</option>
</select>

How to create radio button

<?php echo form_radio($data,$values,$checked=false/true,$extras); ?>

$data (array) : Field  data
$values (string) : Field  value
$checked : whether this radio button being checked
$extras :  Extra attributes
Ex :

<?php echo form_radio('Gender','Male',TRUE)?>
<?php echo form_radio('Gender','Female',FALSE)?>

This will produce like

<input type="radio" name="gender" value="Male" checked="checked">
<input type="radio" name="gender" value="Female">

How to create checkbox

<?php echo form_checkbox($data,$values,$checked=false/true,$extras); ?>

$data (array) : Field  data
$values (string) : Field  value
$checked : whether this check box being checked
$extras :  Extra attributes
Ex :

<?php echo form_checkbox('TermsCondition', 'accept', TRUE); ?>

This will produce like

<input type="checkbox" name="TermsCondition" value="accept" checked="checked">

How to create textarea

<?php echo form_textarea(['name'=>'address','rows'=>'5','cols'=>'10','id'=>'address']);?>

This will produce like

<textarea name="address" rows="5" cols="10" id="address"></textarea>

How to create submit button

<?php echo form_submit('name'=>'submit','value'=>'Submit',class="form-btn");?>

This will produce like

<input type="submit" name="submit" value="Submit" class="form-btn" >

How to create resetbutton

<?php echo form_button('name'=>'reset','value'=>'Reset','type'=>'reset',class="form-btn");?>

This will produce like

<input type="submit" name="submit" value="Submit" class="form-btn" >

How to close a form
form_close function used to close form.

<?php echo form_close( ); ?>

Full Form scripts

First load form helper in application/config/autoload.php.

$autoload['helper']=array('form');

then create a controller  (Signupform.php)

<?php
class Signupform extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){
// loadin from view
$this->load->view("signup_form");
}
}

Now to form view(signup_form.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <!-- This file has been downloaded from Bootsnipp.com. Enjoy! -->
    <title>CodeIgniter Form Tutorial</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</head>
<body>
<section id="login">
    <div class="container">
    	<div class="row">
    	    <div class="col-xs-12">
        	    <div class="form-wrap">
                <h1>Signup Form</h1>
    <?php echo form_open('signupform',['name'=>'signup']);?>
<div class="form-group">
<?php echo form_input(['name'=>'fullname','id'=>'fullname','placeholder'=>'Enter your full name','class'=>'form-control']);?>
</div>
<div class="form-group">
<?php echo form_password(['name'=>'password','id'=>'password','placeholder'=>'Enter your password','class'=>'form-control']);?>
</div>
<div class="form-group">
<?php
$options=array(
'PHP'=>'PHP',
'MySQL'=>'MySQL' ,
'Wordpress'=>'Wordpress' ,
'HTML'=>'HTML'
);
echo form_dropdown('skill',$options,'','class="form-control"');?>
</div>
<div class="form-group">
<?php echo form_radio('Gender','Male',TRUE)?>Male
<?php echo form_radio('Gender','Female',FALSE)?> Female
</div>
<div class="form-group">
<?php echo form_textarea(['name'=>'address','rows'=>'5','cols'=>'10','id'=>'address','class'=>'form-control','placeholder'=>'Enter your address']);?>
</div>
<div class="form-group">
<?php echo form_checkbox('TermsCondition', 'accept', TRUE); ?>Terms & Conditions
</div>
<?php echo form_submit(['name'=>'submit','value'=>'Submit','class'=>'btn btn-custom btn-lg btn-block']);?>
<?php echo form_reset(['name'=>'rest','value'=>'Reset','class'=>'btn btn-custom btn-lg btn-block']);?>
                <?php echo form_close();?>
        	    </div>
    		</div> <!-- /.col-xs-12 -->
    	</div> <!-- /.row -->
    </div> <!-- /.container -->
</section>
</body>
</html>
View Demo
CodeIgniter Form (Download Script)
Size: 2.60 MB
Version: 1

Recommended Tutorials for you


Related posts

Validation rules to a config file in CodeIgniter

Anuj Kumar

Active Record in CodeIgniter

Anuj Kumar

Form Validation in CodeIgniter

Anuj Kumar

How to fetch data in CodeIgniter

Anuj Kumar

CodeIgniter Controllers

Anuj Kumar

CodeIgniter View

Anuj Kumar

CodeIgniter Introduction

Anuj Kumar

CodeIgniter Interview Questions and Answers

Anuj Kumar

CodeIgniter Helpers

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.