0

I am trying to get the flashdata value from one function to another function within the same controller. As soon as I use redirect() it looses its value and shows empty. I tried using both flashdata and session data but nothing is working. Please find my code below:-

public function getUserDetails() 
{
     $first_name = $this->input->post('Uname');
     $last_name = $this->input->post('Ulname');

     //having an insert query

     //I tried with session
     $this->session->set_userdata('fName_sess', $first_name);
     $this->session->set_userdata('lName_sess', $last_name);

      //I tried with flashdata
     $this->session->set_flashdata('fName_flash', $first_name);
     $this->session->set_flashdata('lName_flash', $last_name);

     //I am inserting the value in the database table and $uid is the PK 
    redirect('/User/person/'.$uid);
}


 public function person() 
{
     //with session
     $fN_sess = $this->session->userdata('fName_sess');
     $lN_sess = $this->session->userdata('lName_sess');

     //with keep_flashdata
     $fN_flash = $this->session->keep_flashdata('fName_flash');
     $lN_flash = $this->session->keep_flashdata('lName_flash');

      //also tried with flashdata
     $fN_flash = $this->session->flashdata('fName_flash');
     $lN_flash = $this->session->flashdata('lName_flash');

    echo 'Value with session'. $fN_sess . $lN_sess . '<br>';
    echo 'Value with Flash'  . $fN_flash . $lN_flash ;

    //i have all my views loaded here

}

I am not sure why its not getting the value from one function to another after redirecting it.

Thanks in advance for your time.

Updated:

public function getUserDetails(){
 $first_name = $this->input->post('Uname');
 $last_name = $this->input->post('Ulname');

 //set input post to flash data
 $this->session->set_flashdata('fName_flash', $first_name);
 $this->session->set_flashdata('lName_flash', $last_name);
 redirect('/User/person/');
}


    public function person(){
       $fname =  $this->session->flashdata('fName_flash');
       $lname = $this->session->flashdata('lName_flash');
       if(isset($fname)){ // check if the flash data fname is set. you can do that to lname also.

        $this->load->view(page_header);
        $this->load->view(page_menu);
        $this->load->view(Details);
        $this->load->view(page_footer);
     }

The View Page (Details.php):

<h1> Welcome <?php echo $this->session->flahdata(fName_flash).' ' . $this->session->flahdata(lName_flash) ; ?> </h1>

I came across a post on here where it says to create a path for the sesson (click here).

This is my config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
//$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
$config['sess_encrypt_cookie']  = TRUE; // change this
$config['sess_match_ip']        = TRUE; // change this
$config['sess_match_useragent'] = TRUE; // change this

$config['cookie_prefix']    = '';
$config['cookie_domain']    = 'http://'. $_SERVER['HTTP_HOST'] ; 
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;
Kunal Parekh
  • 380
  • 6
  • 24
  • You need to echo your flash data in your load view and set a condition to make it display. – curiosity Sep 07 '18 at 00:32
  • @curiosity I have echoed in the load view as well, but its not displaying it there. I used var_dump at the the end of my first function, it shows the value but when I used the var_dump in the second function (person), its null and as same as in the view. – Kunal Parekh Sep 07 '18 at 00:36
  • I want this flashdata to perform a select query in the person function, so I need the values from the first function. – Kunal Parekh Sep 07 '18 at 00:38
  • is there any way or anything alternate? – Kunal Parekh Sep 07 '18 at 00:38
  • i see that, your are trying to pass your flash data to another controller right? @Kunal – curiosity Sep 07 '18 at 00:41
  • @curiosity yes that right. I need to have this redirect because if I dont use redirect, the value gets re entered in the database on page refresh. So in the first function i am inserting the data, then pass the data to the second function and perform a search / select query here. Hop you getting what I am trying to say? – Kunal Parekh Sep 07 '18 at 00:44
  • I have also seen few old posts on here regarding the session and the flash data and I have followed them but its not working for me although it may have worked for some of them as it has been upvoted and ticked – Kunal Parekh Sep 07 '18 at 00:46
  • i get what you are trying to explain. – curiosity Sep 07 '18 at 00:49
  • thanks. I am not able to understand why it loses its value on redirect. If i dont use the redirect it shows the value as your suggested solution but when i am refreshing it, it reinserts the data again. Is there any way or something I am not doing right or missing? – Kunal Parekh Sep 07 '18 at 00:54
  • And one more thing, I was not sure about the flashdata that after setting the flashdata, if i am using `keep_flashdata();` is this done in the first function right? – Kunal Parekh Sep 07 '18 at 00:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179604/discussion-between-kunal-parekh-and-curiosity). – Kunal Parekh Sep 07 '18 at 01:29

1 Answers1

0

In order to do that, you need to do this...

public function getUserDetails(){
 $first_name = $this->input->post('Uname');
 $last_name = $this->input->post('Ulname');

 //set input post to flash data
 $this->session->set_flashdata('fName_flash', $first_name);
 $this->session->set_flashdata('lName_flash', $last_name);
 redirect('User/person/');
}


public function person(){
   $fname =  $this->session->flashdata('fName_flash');
   $lname = $this->session->flashdata('lName_flash');
   if(isset($fname)){ // check if the flash data fname is set. you can do that to lname also.

    //load views here...
    //if you want to echo it inside your **view.php** file do this
    $data['fname'] = $fname;
    $data['lname'] = $lname;
    $this->load->view('view', $data); //pass the data to view

 }

then echo it like this....

echo $fname ."<br>";
echo $lname;
//i already test it. and it's working!

hope that helps...

curiosity
  • 834
  • 8
  • 20
  • Nope its not working. The if condition $fname does not allow to load the view. Which I think, the value is null – Kunal Parekh Sep 07 '18 at 01:19
  • i think my answer is right. maybe you echo it inside the view incorrectly. – curiosity Sep 07 '18 at 01:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179605/discussion-between-kunal-parekh-and-curiosity). – Kunal Parekh Sep 07 '18 at 01:56
  • I completely understand u getting the results, I did the var_dump for the value for $fname and it came as NULL. I tried this before going inside the if statements. And for some how my value passed in 2nd function is blank – Kunal Parekh Sep 07 '18 at 03:36
  • i don't know why is it not working on you. i did it with the test already. – curiosity Sep 07 '18 at 03:46
  • I have used updated my question as I came across a post. I am not sure where i am going wrong or whats happening. – Kunal Parekh Sep 07 '18 at 03:57