0

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'] = '[email protected]'; 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 [email protected], 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. :)

2 Answers 2

1

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 be alert("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

Sign up to request clarification or add additional context in comments.

Comments

1

Thats because the alert string is generated when the page is loaded. And when you make an ajax request it still doesnt change, because its not refreshed. On ajax success you should output the responseText and then it will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.