Generate form using form helper class in php with codeigniter framework , to generate dynamic form. Loading this Helper
This helper is loaded using the following code (to controller class):
$this->load->helper('form');
Here’s a simple example:
The above example would create a form that points to your base URL plus the “email/send” URI
segments, like this:
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
Returns:
An HTML text input field tag
Return type:
string
Lets you generate a standard text input field. You can minimally pass the field name and value in the first and second parameter:
echo form_input('username', 'jack');
Or you can pass an associative array containing any data you wish your form to contain:
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'jack',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%'
);
echo form_input($data);
/*
Would produce:
<input type="text" name="username" value="jack" id="username" maxlength="100" size="50" style="width:50%" />
*/
Example
First Load the form help in controller class
This helper is loaded using the following code (to controller class):
$this->load->helper('form');
Here’s a simple example:
echo form_open('home');
The above example would create a form that points to your base URL plus the “email/send” URI
segments, like this:
<form method="post" accept-charset="utf-8" action="http://example.com/index.php/home">
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
Returns:
An HTML text input field tag
Return type:
string
Lets you generate a standard text input field. You can minimally pass the field name and value in the first and second parameter:
echo form_input('username', 'jack');
Or you can pass an associative array containing any data you wish your form to contain:
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'jack',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%'
);
echo form_input($data);
/*
Would produce:
<input type="text" name="username" value="jack" id="username" maxlength="100" size="50" style="width:50%" />
*/
Example
First Load the form help in controller class
add application/controller/Home.php
<?php
// First we created controller to call
class Home extends CI_Controller {
//Here we can call model and helper class
public function __construct()
{
parent::__construct();
$this->load->model('emp_model');
$this->load->helper('url_helper');
$this->load->helper('form');
}
//default function
public function index() {
// First we created controller to call
class Home extends CI_Controller {
//Here we can call model and helper class
public function __construct()
{
parent::__construct();
$this->load->model('emp_model');
$this->load->helper('url_helper');
$this->load->helper('form');
}
//default function
public function index() {
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
//here we are getting a value from database
$data['employee'] = $this->emp_model->get_emp();
$this->load->view('pages/'.$page, $data);
}
}
add form to the view
application/view/pages/home.php
<h1>Add Employee </h1>
<!-- pass the hidden value in the form -->
<?php $hidden = array('username' => 'Jack', 'member_id' => '123');
echo form_open('home', '', $hidden);
//home is action
$name = array(
'type' => 'text',
'name' => 'name',
'id' => 'name',
'placeholder' => 'Enter Name',
'class' => 'hiddenname'
);
$email= array(
'type' => 'text',
'name' => 'email',
'id' => 'hiddenemail',
'placeholder' => 'Enter Email',
'class' => 'hiddenemail'
);
$address = array(
'type' => 'text',
'name' => 'address',
'id' => 'address',
'placeholder' => 'Enter address',
'class' => 'address'
);
//here three variable created lets create new one contact
$contact =array(
'type' => 'text',
'name' => 'contact',
'id' => 'contact',
'placeholder' => 'contact'
);
echo form_input($name);
echo form_input($email);
echo form_input($address);
echo form_input($contact);
echo form_submit('btnSubmit', 'Submit')
?>
</form>
No comments:
Post a Comment