I am currently writing an “Edit Account” module. Using jQuery.post(); I can successfully update my database, and I can write to the session variables from the PHP script – however,
when trying to access the Session variable that I just wrote to, the data does not seem to have been updated.
I write to the session like this: $_SESSION['email'] = 'john@doe.com'; in my Ajax.php
My jQuery (minified) callback on success:
$.post(...,function(res)
{
if(res=='success')
{
// Alert the new E-Mail, read from the Session!
alert("<?php echo $_SESSION['email']; ?>");
}
}
However, the alert does not output john@doe.com, it outputs whatever the E-Mail was it was when the entire page was loaded. When I refresh the page, the session data has been properly updated.
I do have session_start(); on both index.php (my site) and in Ajax.php
If you need more info please do let me know. 🙂
First of all it should be
alert("<?php echo $_SESSION['email']; ?>");and secondly $.post is dynamic but
<?php echo $_SESSION['email']; ?>is static. It renders once, on load, not everytime you make an .post() so if$_SESSION['email'] = 'blah';by the time the page is loaded, the alert will always bealert("blah");, until you reload the page.If you want to alert the new session variable you have to make a new ajax request to a php file (ie. return_session.php?key=email ), that will return the new session variable