Welcome Guest, Not a member yet? Register   Sign In
Problems with flashdata()
#1

[eluser]yemaw[/eluser]
Hi everybody,
I tried to use flashdata() function in my codes to pass message to users from controller file to view file.
Actually the message might be rise one time and next times it mustn't rise. Right?
But now, my problem is opposite.
The message not rise on first time and rise on next everytimes. Sad

my code is
//controller
Code:
//Username and password are incorrect
  else{
     $data['page_title'] = "Login Again";
     $this->session->set_flashdata('msg','incorrect');
     $this->load->view('templates/welcome',$data);
  }

//view
Code:
<?php echo $this->session->flashdata('msg'); ?>

Thanks!
#2

[eluser]darrentaytay[/eluser]
Flashdata is available on the next server request only - so when you are setting the flashdata and then outputting your view, you aren't making a new server request.

Why don't you just pass the message directly to the view?

Controller:
Code:
//Username and password are incorrect
  else{
     $data['page_title'] = "Login Again";
     $data['message'] = "Login details not valid.";
     $this->load->view('templates/welcome',$data);
  }
View:
Code:
<?php echo $message ?>
#3

[eluser]yemaw[/eluser]
Thanks!
Yes i can pass message directly to view, but notice is rise before first time login attempt and it is also still rise in the next refresh times.

Like this.

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: message
Filename: templates/welcome.php
Line Number: 13

But I fixed it like this.In view file line13
Code:
<?php echo @$message;$message=NULL; ?>
But I think it'll be better way to pass messages.
#4

[eluser]darrentaytay[/eluser]
Looking at your code, this message should only show if a user submits incorrect details?

Also, this should work:

Code:
<?php echo isset($message) ? $message : FALSE; ?>
#5

[eluser]yemaw[/eluser]
It works.
But in addition, some situations like this.
A user registered and we need to redirect to login page and we need to say "You are now a member of blah blah blah and you can now login". The code may be something like this,

Code:
if($this->user_register_model->create_user()){
   redirect(site_url());
}
But in this condition we can't pass the message from controller to view. So, is there anymore ways to pass data especially to work together with redirect();

Thanks! darrentaytay, By the way, ur blog is cool, by i need rss button.
#6

[eluser]darrentaytay[/eluser]
Flashdata will work perfectly with Redirect :-)
#7

[eluser]yemaw[/eluser]
Ohhh!! Got it, Thanks
#8

[eluser]InsiteFX[/eluser]
If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() function.
Code:
$this->session->keep_flashdata('item');

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB