Codeigniter Form Validation
![]() |
Codeigniter Form Validation |
Hi Guys in this lesson we are going to learn simple form validation in codeigniter..
Just follow the below codeigniter form validation example..
Configuration:
application/config/autoload.php
$autoload['helper'] = array('url');
Base_Url:
$config['base_url'] = 'http://localhost/codeigniter/';
Controllers:
application/controllers/blog.php
<?php class Blog extends CI_Controller { // You can autoload helpers and libraries etc if you need to, public function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->helper('email'); } public function index() { $this->load->view('form'); } public function validate() { $this->form_validation->set_rules('name', 'name', 'trim|required'); $this->form_validation->set_rules('username', 'username', 'trim|required'); $this->form_validation->set_rules('email', 'email', 'trim|required|valid_email'); if ($this->form_validation->run() == FALSE) { // Error $this->load->view('form'); } else { // Success $data['success']='You Form Validation Successful'; $this->load->view('form',$data); } } }Views:
application/views/form.php
<style> .error{ color:red; font-style:italic; font-weight:bold; } </style> <h2>Codeigniter Form Validation Example</h2> <form method="POST" action="<?php echo base_url();?>index.php/blog/validate"> <table> <tr> <td>Name</td> <td> <input type="text" name="name"> <?php echo form_error('name', '<div class="error">', '</div>'); ?> </td> </tr> <tr> <td>UserName:</td> <td><input type="text" name="username"> <?php echo form_error('username', '<div class="error">', '</div>'); ?> </td> </tr> <tr> <td>Email:</td> <td> <input type="text" name="email"> <?php echo form_error('email', '<div class="error">', '</div>'); ?> </td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Submit"/></td> </tr> </table> </form> <?php if(isset($success)) echo $success;?>
Now Run http://localhost/codeigniter/index.php/blog
and Enjoy.. Keep visiting for more codes and Codeigniter Examples..
Please Leave your comment .Thank You...
What's Taking place i am new to this, I stumbled upon this I have discovered It absolutely helpful and it has aided me out loads.
ReplyDeleteI am hoping to contribute & assist different users like its helped me.
Great job.