how to create form in codeigniter
how to create form in codeigniter
1) If you want to create a simple form use codeigniter there are some procedure use
Create a file in view (form.php) and write these code here
<h2>Simple form in codeigniter</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('Welcome/add'); ?>
<table class="table">
<tr>
<td><input type="text" name="name"></td>
<td><input type="tex" name="lastname"></td>
<br>
</tr>
<tr>
<td><button type="submit" name="add" class="btn btn-danger">Add</button></td>
</tr>
</table>
</form>
2)create a controller file in controlller folder(form_controller.php)
here load library and hellper
you can also load library in autoload
open config folder and open autoload file
and see this
i)$autoload['libraries'] = array();
ii)$autoload['helper'] = array();
write code
i)$autoload['libraries'] = array();
ii)$autoload['helper'] = array('form');
you can also autoload multiple helper
$autoload['helper'] = array('form','url','',------,'');
form_controller.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$this->load->view('index');
}
public function add(){
/* Load form validation library */
$this->load->library('form_validation');
$fname = $this->input->POST('name');
$lastname = $this->input->POST('lastname');
$this->form_validation->set_rules('name', 'name', 'required');
$this->form_validation->set_rules('lastname', 'lastname', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('index');
}
else {
$this->load->view('sucess');
}
}
}
No comments