how to pass data from view to controller in codeigniter?

Today, We want to share with you how to pass data from view to controller in codeigniter.In this post we will show you how to pass data from model to view in codeigniter, hear for how to pass data from controller to view in php we will give you demo and example for implement.In this post, we will learn about How To Pass Array From View To Controller? with an example.

Pass Data Between View and Controller in CodeIgniter

codeigniter Controller pass parameter route Function
codeigniter Controller pass parameter route Function

Step 1: View to Controller
<!doctype html>
<html>
 <body>
   <form method='post' action='<?= base_url(); ?>'>

     <table>
       <tr>
         <td>Name</td>
         <td><input type='text' name='member_name'></td>
       </tr>
       <tr>
         <td>Membername</td>
         <td><input type='text' name='member_profile_name'></td>
       </tr>
       <tr>
         <td>Email</td>
         <td><input type='text' name='member_email'></td>
       </tr>
       <tr>
         <td> </td>
         <td><input type='submit' name='submit' value='Submit'></td>
       </tr>
    </table>
 
   </form>
 </body>
</html>

Step 2: Controller

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

class Member 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['member_name']."<br/>";
     echo "<b>Membername :</b> ".$postData['member_profile_name']."<br/>";
     echo "<b>Email :</b> ".$postData['member_email']."<br/>";
 
   }

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

}

Controller to View

View Code

<!doctype html>
<html>
 <body>
   <div>
   <?php
   if(isset($response)){
      echo "<b>Name :</b> ".$response['member_name']."<br/>";
      echo "<b>Membername :</b> ".$response['member_profile_name']."<br/>";
      echo "<b>Email :</b> ".$response['member_email']."<br/>";
   }
   ?>
   </div>

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

     <table>
       <tr>
          <td>Name</td>
          <td><input type='text' name='member_name'></td>
       </tr>
       <tr>
          <td>Membername</td>
          <td><input type='text' name='member_profile_name'></td>
       </tr>
       <tr>
          <td>Email</td>
          <td><input type='text' name='member_email'></td>
       </tr>
       <tr>
          <td> </td>
          <td><input type='submit' name='submit' value='Submit'></td>
       </tr>
     </table>
 
   </form>
 </body>
</html>

Controller Code

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

class Member 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('member_view',$data);
 
 }

}

3. By URL

View Code

<!doctype html>
<html>
 <body>
   <div>
    <?= "memberid : ".$memberid."<br/>" ?>
    <?= "membername : ".$membername."<br/>" ?>
   </div>
   <table border='1'>
     <tr>
       <th>S.no</th>
       <th>Name</th>
       <th>Membername</th>
       <th> </th>
     </tr>
     <tr>
       <td>1</td>
       <td>Virat Kohali</td>
       <td>viratkohali</td>
       <td><a href='<?= base_url(); ?>index.php/member/index/1/viratkohali'>Edit</a></td>
     </tr>
     <tr>
       <td>2</td>
       <td>Bhavika Pethani</td>
       <td>bhavikapethani</td>
       <td><a href='<?= base_url(); ?>index.php/member/index/2/bhavikapethani'>Edit</a></td>
     </tr>
     <tr>
       <td>3</td>
       <td>Jigar Shah</td>
       <td>jigarshah</td>
       <td><a href='<?= base_url(); ?>index.php/member/index/3/jigarshah'>Edit</a></td>
     </tr>
   </table>

  </body>
</html>

Controller Code

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

class Member extends CI_Controller {

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

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

  public function index($memberid = 0, $membername = ""){

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

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

}

I hope you get an idea about how to pass value in url in codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  PHP Laravel Full Text Search in MYSQL Database

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *