Welcome Guest, Not a member yet? Register   Sign In
set_flashdata stays open
#1

I am developing a web project which I am about to finish, using CodeIgniter 3. In this web application I am developing I am using flash messages to indicate to the user that certain operations have been performed successfully, for example, when logging in, logging out, editing an entry, etc...
The problem is that, even though I do redirect after creating the messages, they stay on screen all the time, even when I switch from one link to another.

I have searched for information and I have seen that it is necessary to make redirect after the creation of each one of the alerts, but I do that correctly and still they remain on screen all the time. Sometimes I even, I don't know why but, I shut down the server, restart the computer and when I log in these messages are still on screen, when they should be removed the next time I click on a link or reload the page.

This is the code for the logout method, wich displays a message that says that the operation was performed successfully. (Sorry is in spanish but the code is very legible).


Code:
public function logout(){
            //Unset data
            $this->session->unset_userdata('logged_id');
            $this->session->unset_userdata('user_id');
            $this->session->unset_userdata('username');

            //mensaje
            $this->session->set_flashdata('usuario_logout','Has cerrado sesión. ¡Hasta pronto!');

            redirect('usuarios/login');

        }

And here is the part of the header that displays the messages (in header.php):


Code:
<div class="container">
    <?php if($this->session->flashdata('usuario_logout')): ?>
      <?php echo '<p class="alert alert-info">'.$this->session->flashdata('usuario_logout').'</p>'; ?>
    <?php endif; ?>
</div>

Do any of you know what is going on here and why the messages are not disappearing?

Thank you in advance!
Reply
#2

@pedreitor,

Have you thought about using notification libraries like toastr (https://codeseven.github.io/toastr/) or sweet alerts (https://sweetalert.js.org/)?
Reply
#3

(03-16-2021, 06:55 AM)php_rocs Wrote: @pedreitor,

Have you thought about using notification libraries like toastr (https://codeseven.github.io/toastr/) or sweet alerts (https://sweetalert.js.org/)?
Hi, php_rocs.
I was not aware of any of the options you mention.
I will consider the option of using them. According to your criteria, which one do you think is better or easier to install and use?
Thank you for your response.
Reply
#4

@pedreitor,

Both of them are pretty easy to install and use. Toastr seems to be the better fit for what you appear to want which is a message set with a timer. Where as sweet alerts expects a response from the user.
Reply
#5

(03-18-2021, 08:04 AM)php_rocs Wrote: @pedreitor,

Both of them are pretty easy to install and use.  Toastr seems to be the better fit for what you appear to want which is a message set with a timer.  Where as sweet alerts expects a response from the user.
Hi again.

Sorry for the insistence but I think that I do not import well any of the two libraries because I keep getting errors when using the methods that call the alerts in both options.
For example, to use toastr, what I do is to put in the header the following lines:

<link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>



Then, i call the alert in the User controller (for example) for the logout method like this:

Code:
// Display an info toast with no title
toastr.info('Log in completed.')
 I followed the instructions described in the official page that you send me, but when I click the logout button, this happens:

An
uncaught exception was encountered
Type: error
Message: Undefined constant "toastr"

Something like that happens when I try to use SweetAlert, I do the same with the CDN and when I call the alert using the "swal" method, it says that the swal method is not defined.

Sorry if this is too basic, but i'm starting in CodeIgniter and I did't use too many stuff like this, and I don't know what to do.
Thanks in advance again.
Reply
#6

(This post was last modified: 03-18-2021, 02:28 PM by php_rocs.)

@pedreitor,

This line should be at the footer of the page. Not the header.
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>

Try the above.
see example code: view-source:https://codeseven.github.io/toastr/demo.html
Reply
#7

(03-18-2021, 02:27 PM)php_rocs Wrote: @pedreitor,

This line should be at the footer of the page.  Not the header.
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>

Try the above.
see example code: view-source:https://codeseven.github.io/toastr/demo.html
Hi again, php_rocs.

I have managed to make it work, with SweetAlert I have managed to get the messages to appear correctly on the screen. But the original problem reappears.

It seems that the set_flashdata messages remain active during all the time, because when I redirect the page or change to another link, the SweetAlert message appears again.

This leads me to think that somehow these flash_data messages stay in the session and the code detects that it has just arrived, so it shows the message again on the screen.

Any idea why this is happening?

Thank you in advance again! You are helping me so much.
Reply
#8

(03-22-2021, 12:12 PM)pedreitor Wrote:
(03-18-2021, 02:27 PM)php_rocs Wrote: @pedreitor,

This line should be at the footer of the page.  Not the header.
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>

Try the above.
see example code: view-source:https://codeseven.github.io/toastr/demo.html
Hi again, php_rocs.

I have managed to make it work, with SweetAlert I have managed to get the messages to appear correctly on the screen. But the original problem reappears.

It seems that the set_flashdata messages remain active during all the time, because when I redirect the page or change to another link, the SweetAlert message appears again.

This leads me to think that somehow these flash_data messages stay in the session and the code detects that it has just arrived, so it shows the message again on the screen.

Any idea why this is happening?

Thank you in advance again! You are helping me so much.
Hi, I have more news.

I have tried doing a print_r of $this->session->flashdata on the top of the header, inside de body tag (in wich the flashdata are displayed), and this is what I found.

Array ( [usuario_logout] => [usuario_login] => [usuario_login_fallido] => [partido_actualizado] => El partido ha sido actualizado con éxito. [competicion_actualizada] => La competición ha sido actualizada con éxito. )

The messages appear in spanish because I'm from Spain. I try to translate as much as I could, but in this case, I think that is not important.
The thing is that it doesn't seem to clear the variable after each redirect, so it concatenates each message, one after the other, and I think that's why it shows all of them at once, instead of just showing the last message and clearing itself.

I really don't have a clue of what to do here. I'm really stuck on this.
Any help is appreciated.
Thanks in advance.
Reply
#9

Did you try to clear your web browser's cache to see if it is a caching problem?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#10

(03-22-2021, 08:45 PM)InsiteFX Wrote: Did you try to clear your web browser's cache to see if it is a caching problem?
Hi InsiteFX,
I have tried running the web application in incognito mode, and absolutely nothing happens. Not a single alert.
On the other hand, I've tried in Mozilla and Internet Explorer too, and the same happens, it display de alert and it stays in the array of flashdata as long as the application is running.
Is there any other way to clear the cache even manually to try clearing it after the redirect?
Thanks for the reply.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB