set_flashdata stays open |
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(){ And here is the part of the header that displays the messages (in header.php): Code: <div class="container"> Do any of you know what is going on here and why the messages are not disappearing? Thank you in advance!
@pedreitor,
Have you thought about using notification libraries like toastr (https://codeseven.github.io/toastr/) or sweet alerts (https://sweetalert.js.org/)? (03-16-2021, 06:55 AM)php_rocs Wrote: @pedreitor,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. (03-18-2021, 08:04 AM)php_rocs Wrote: @pedreitor,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 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.
@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 (03-18-2021, 02:27 PM)php_rocs Wrote: @pedreitor,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. (03-22-2021, 12:12 PM)pedreitor Wrote:Hi, I have more news.(03-18-2021, 02:27 PM)php_rocs Wrote: @pedreitor,Hi again, php_rocs. 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.
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 )
(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. |
Welcome Guest, Not a member yet? Register Sign In |