Form Tag
How to create HTML form

To create form in codeigniter, codeigniter provide us a function to create form that is very secure for us.

First Load Form Helper

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

Use this code in your webpage

echo form_open('email/send');

this code create html form where action is email/send.

Complete code to create html form

Adding form helper in controller

Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

public function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');

}

public function index()
{
$this->load->view('test');
}
}

View

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html>
<head></head>
<body>
<?php echo form_open('email/send'); ?>
</form>
</body>
</html>

Adding Attributes

<?php
echo form_open('email/send', 'class="email" id="myform"');
echo form_close();
?>

Output

<form action="http://localhost/codeigniter/index.php/email/send" class="email" id="myform" method="post" accept-charset="utf-8">
</form>

Multipart form opening tag

<?php
echo form_open_multipart('email/send', 'class="email" id="myform"');
echo form_close();
?>

Output

<form action="http://localhost/codeigniter/index.php/email/send" class="email" id="myform" enctype="multipart/form-data" method="post" accept-charset="utf-8">
</form>

Creating Hidden Field

<?php
echo form_open_multipart('email/send', 'class="email" id="myform"');
$data = array(
'name' => 'Shishir Developer',
'email' => 'shishirdeveloper@gmail.com',
'url' => 'http://shishirdeveloper.com'
);
echo form_hidden($data);
echo form_close();
?>

Output

<form action="http://localhost/codeigniter/index.php/email/send" class="email" id="myform" enctype="multipart/form-data" method="post" accept-charset="utf-8">

<input type="hidden" name="name" value="Shishir Developer" />
<input type="hidden" name="email" value="shishirdeveloper@gmail.com" />
<input type="hidden" name="url" value="http://shishirdeveloper.com" />
</form>

Form other control values

form_password()
form_upload()
form_textarea()
form_dropdown()
form_multiselect()
form_checkbox()
form_radio()
form_submit()
form_reset()
form_button()
form_close()


Previous
Next