Wednesday, March 9, 2022

[FIXED] CodeIgniter: how to pass values from a specific function to another one

Issue

I am trying to pass a value coming from a form using the method post from a function to another one.

class Send_value extends CI_Controller {

public function main_page() {
     $data['user'] = $this->input->post('fullName');
      ...
}


public function welcome_page(){
  //Now I would like to pass $data['user'] here.      

}
}

How can I pass it in the second function?


Solution

You need to pass parameter within that function and need to define that parameter as

class Send_value extends CI_Controller {

public function main_page() {
     $data['user'] = $this->input->post('fullName');
     echo $data['user']; //echoing value from post
     if(!empty($data)){
        $this->welcome_page($data); //passing data into another function
     } else {
        echo "Didn't get value from post";
     }
}


public function welcome_page($data = ''){
                           //^^ parameter set within function
    if(is_array($data) && count($data) > 0){
       print_r($data);
    } else {
       echo "No result found";
    }
  }
}


Answered By - Narendrasingh Sisodia

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.