Pass Data Between View and Controller in CodeIgniter

HTML form is one of the common elements within the webpage which allows the user to input data and submit it for processing.

The data is also passed by URL which is good for limited and less sensitive information.

In this tutorial, I show how you can pass data between view and controller.

Pass Data Between View and Controller in CodeIgniter


Contents

  1. View to Controller
  2. Controller to View
  3. By URL
  4. Conclusion

1. View to Controller

Using <form > to pass data from view to controller.

View

Created a <form method='post'> which contains 3 textboxes and submit button.

<!doctype html>
<html>
 <body>
   <form method='post' action='<?= base_url(); ?>'>

     <table>
       <tr>
         <td>Name</td>
         <td><input type='text' name='txt_name'></td>
       </tr>
       <tr>
         <td>Username</td>
         <td><input type='text' name='txt_uname'></td>
       </tr>
       <tr>
         <td>Email</td>
         <td><input type='text' name='txt_email'></td>
       </tr>
       <tr>
         <td>&nbsp;</td>
         <td><input type='submit' name='submit' value='Submit'></td>
       </tr>
    </table>
 
   </form>
 </body>
</html>

Controller

The $this->input->post() method returns an Associative array of submitted values.

Using this to read other post values and print on the screen.

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

class User extends CI_Controller {

 public function __construct() {

   parent::__construct();

   // load base_url
   $this->load->helper('url');
 }

 public function index(){

   // Check form submit or not
   if($this->input->post('submit') != NULL ){
 
     // POST data
     $postData = $this->input->post();

     // Read POST data
     echo "<b>Name :</b> ".$postData['txt_name']."<br/>";
     echo "<b>Username :</b> ".$postData['txt_uname']."<br/>";
     echo "<b>Email :</b> ".$postData['txt_email']."<br/>";
 
   }

   // load view
   $this->load->view('user_view');
 
 }

}

NOTE – If you are using <form method='get'> on the webpage then use $this->input->get() method to read values.

Demo

Fill the form and click the submit button.


2. Controller to View

View

With $response display data on the page which is initialized with the controller when <form> is submitted and passed to the view.

<!doctype html>
<html>
 <body>
   <div>
   <?php
   if(isset($response)){
      echo "<b>Name :</b> ".$response['txt_name']."<br/>";
      echo "<b>Username :</b> ".$response['txt_uname']."<br/>";
      echo "<b>Email :</b> ".$response['txt_email']."<br/>";
   }
   ?>
   </div>

   <form method='post' action='<?php echo base_url(); ?>'>

     <table>
       <tr>
          <td>Name</td>
          <td><input type='text' name='txt_name'></td>
       </tr>
       <tr>
          <td>Username</td>
          <td><input type='text' name='txt_uname'></td>
       </tr>
       <tr>
          <td>Email</td>
          <td><input type='text' name='txt_email'></td>
       </tr>
       <tr>
          <td>&nbsp;</td>
          <td><input type='submit' name='submit' value='Submit'></td>
       </tr>
     </table>
 
   </form>
 </body>
</html>

Controller

Initialize $data['response'] with the post data and pass the $data in $this->load->view() method.

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

class User extends CI_Controller {

 public function __construct() {

   parent::__construct();

   // load base_url
   $this->load->helper('url');
 }

 public function index(){
   $data = array();
   // Check form submit or not
   if($this->input->post('submit') != NULL ){
     
     // POST data
     $postData = $this->input->post();

     $data['response'] = $postData;
 
   }

   // load view
   $this->load->view('user_view',$data);
 
 }

}

Demo

Fill the form and click the submit button.


3. By URL

View

Display users list in the <table> and add Edit option. In the edit link set the controller method path and pass the two parameters – userid/username.

<!doctype html>
<html>
 <body>
   <div>
    <?= "userid : ".$userid."<br/>" ?>
    <?= "username : ".$username."<br/>" ?>
   </div>
   <table border='1'>
     <tr>
       <th>S.no</th>
       <th>Name</th>
       <th>Username</th>
       <th>&nbsp;</th>
     </tr>
     <tr>
       <td>1</td>
       <td>Yogesh Singh</td>
       <td>yssyogesh</td>
       <td><a href='<?= base_url(); ?>index.php/user/index/1/yssyogesh'>Edit</a></td>
     </tr>
     <tr>
       <td>2</td>
       <td>Sonarika Bhadoria</td>
       <td>bsonarika</td>
       <td><a href='<?= base_url(); ?>index.php/user/index/2/bsonarika'>Edit</a></td>
     </tr>
     <tr>
       <td>3</td>
       <td>Anil Singh</td>
       <td>anil</td>
       <td><a href='<?= base_url(); ?>index.php/user/index/3/anil'>Edit</a></td>
     </tr>
   </table>

  </body>
</html>

Controller

There are two ways to read URL value.

  • Parameter – Pass the extra parameter in the controller method according to the number of the parameter passed in the URL.
  • segment() method – You don’t need to pass the extra parameter to the controller method. With $this->uri>segment(n) method retrieve the information from the URL string.

Within the URL the n=1 controller-name, n=2 method-name+, n=3 paramter 1, n=4 paramter 2,etc.

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

class User extends CI_Controller {

  public function __construct() {
    
    parent::__construct();

    // load base_url
    $this->load->helper('url');
  }

  public function index($userid = 0, $username = ""){

    // $userid = $this->uri->segment(3);
    // $username = $this->uri->segment(4);
 
    $data['userid'] = $userid;
    $data['username'] = $username;

    // load view
    $this->load->view('user_view',$data);
 
  }

}

Demo

Click on the Edit link. Open in a new tab.


4. Conclusion

Pass the second parameter from the controller to pass data to the View and you can use either segment() method or read the value from the controller method parameter for retrieving the value from URL string.

If you found this tutorial helpful then don't forget to share.