CodeIgniter Forums
session()->has('???') always blank in view file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: session()->has('???') always blank in view file (/showthread.php?tid=76798)



session()->has('???') always blank in view file - kristianlake - 06-21-2020

Hi, 

I have some code in a $.ajax call and under success: I am using the following code

<?php if (session()->has('success')) : ?>
    console.log("success");
<?php endif ?>


success is set and i can debug and see it returns true.

The problem is this line of code seems to always return blank and never get to my console.log.

I am quite new to everything, this could well be some PHP'ism I have not yet learnt/understood, But I have spent a few hours in the debugger trying to echo the variable back to console.log with no joy. The only joy i get is in the debugger (see attached screenshot).

Could someone guide me in the right direction here? Smile

Thanks 


RE: session()->has('???') always blank in view file - dave friend - 06-22-2020

If I understand correctly the code you show is inside of the ajax.success callback. If that is true then the problem is that the PHP will only be evaluated once when the page is first loaded but not when the javascript callback executes.

Instead of pulling the value from the session, you should return the value to ajax.success.


RE: session()->has('???') always blank in view file - kristianlake - 06-22-2020

perfect Dave, you were spot on.

My ajax calls were not refreshing the page or redirecting, so once i pulled the value down properly it was working great. Thanks for the tip.