VALIDATION FORM IN CODEIGNITER USING SESSION LIBRARY

Date :28/03/2019

Introduction:

The following tutorial implementing form validation using session library in codeigniter.

First of all, we should create database and table. we can create database client and we can create table form .

To implement the form validation we need to create four steps.

step1:

We have to create the form in view and this is considered as our default form.

In this form we are entering the data.Those data validated to the following steps.

we can save it as myform.php(application/view/myform.php).

<html>
   <head>
      <title>My Form</title>
   </head>
   <body>
      <!-- <form action = ""> -->
      <?php echo form_open('Form/index1'); ?>  
      <h5>firstname</h5>
      <input type = "text" name = "firstname" value="">
      <h5>gender</h5>
      <input type = "text" name = "gender" value="">  
      <h5>hobbies</h5>
      <input type = "text" name = "hobbies" value="">
      <h5>district</h5>
      <input type = "text" name = "district" value="">
      <h5>discription</h5>
      <input type = "text" name = "discription"  value="">        
      
<?php echo form_close(); ?> </body> </html>

The above code Create the simple form.

step2:

Then we create the controller page and check the validation in that page.

Above data validated in this controller part. we can save it as Form.php(application/controller/Form.php).

<?php
   class Form extends CI_Controller { 
   
      public function index() { 
         
         #Used to load the form library
         $this->load->helper(array('form', 'url'));
   
         #Used to load the session library
         $this->load->library("session");
   
         #Used to load the view library
         $this->load->view('myform');
         
      }
   public function index1()
   {
      
      $this->load->library("session");
   
      #used to load the form validation library
      #the form validation library used to load the set_rules functions
      $this->load->library('form_validation');
   
      
      #the following functions check wheather entered details are write or wrong
      $this->form_validation->set_rules('firstname', 'firstname', 'required');
      $this->form_validation->set_rules('gender', 'gender', 'required');
      $this->form_validation->set_rules('hobbies', 'hobbies', 'required');
      $this->form_validation->set_rules('district', 'district', 'required');
      $this->form_validation->set_rules('discription', 'discription', 'required');
   
   #the above validation is fail it enter into the following if condition
   if ($this->form_validation->run() == FALSE) 
   {    
        $postData = array(
                'firstname' => $this->input->post('firstname'),
                'gender' => $this->input->post('gender'),
                  'hobbies' => $this->input->post('hobbies'),
                    'district' => $this->input->post('district'),
                      'discription' => $this->input->post('discription')
                 );   
      
       echo json_encode($this->form_validation->error_array());  
      $this->session->set_userdata($postData);
      $this->load->view('myform1');
       
   } 
   
   #the validation part is pass the else part is executed
   else 
   {
   
   $this->load->model('Insert_model');
          $postData = array(
                'firstname' => $this->input->post('firstname'),
                'gender' => $this->input->post('gender'),
                  'hobbies' => $this->input->post('hobbies'),
                    'district' => $this->input->post('district'),
                      'discription' => $this->input->post('discription')
                 );  
         print_r($insert = $this->Insert_model->insert($postData)); 
          echo "Data inserted successfully";
       }
         }
   
   
       
   }
   ?> 

The above code is validate our data.

     * $this->load->helper(array('form', 'url')); = Used to load the form library.

     * $this->load->library("session"); = Used to load the session library.

     * $this->load->library('form_validation'); = Used to load the form validation library. 

step3:

Again we create a another view page for reload the form.

This form loaded when the given details is wrong and it shows error message we can save it as myform1.php(application/view/myform1.php).

<html>
   <head>
      <title>My Form</title>
   </head>
   <body>
      <!-- <form action = ""> -->
      <?php echo form_open('Form/index1'); ?>  
      <h5>firstname</h5>
      <input type = "text" name = "firstname" value="<?php echo $this->session->userdata('firstname'); ?>">
      <h5>gender</h5>
      <input type = "text" name = "gender" value="<?php echo $this->session->userdata('gender'); ?>">  
      <h5>hobbies</h5>
      <input type = "text" name = "hobbies" value="<?php echo $this->session->userdata('hobbies'); ?>">
      <h5>district</h5>
      <input type = "text" name = "district" value="<?php echo $this->session->userdata('district'); ?>">
      <h5>discription</h5>
      <input type = "text" name = "discription"  value="<?php echo $this->session->userdata('discription'); ?>">        
      
<?php echo form_close(); ?> </body> </html>

The above code create the another form.It contain our data.

Step4:

Here we have to create the mode form our database.

We can save it as a Insertmodel.php (application/model/Insert_model.php).

class Insert_model extends CI_Model 
{
	public function insert($data = array()) 
	{
        #here form is our table name
        $insert = $this->db->insert('form', $data);
    }
}

The above code connect our form with database.

  • Finally The all above there steps completed form will displayed.
    we can fill the form and click the submit.
  • If we missed any columns, It will display another form(myform1.php). The form containing our data along with error message.
  • Otherwise the data inserted into our database.Then it display success message like Data inserted successfully.

Conclusion:

The process continue untill we submit correct data.if we enter the correct
data it inserted into our database.and show success message.

Thanks for using pheonixsolutions.

You find this tutorial helpful? Share with your friends to keep it alive.

1 thought on “VALIDATION FORM IN CODEIGNITER USING SESSION LIBRARY”

Leave a Reply