(jquery) sessions

I have a button that calls a php file that sets a session using ajax.

is it possible to echo the results of that session without refreshing the page?

Yes. The success method of the ajax call is where you can specify a function that accepts data from the php file, that data just being the text that the script echos out.

Hi thank you for your response.

I would like to echo the session which was just set, however it won’t detect the new session without refreshing the page.

It won’t? That’s odd, because this simple test seems to do that just fine.


$setSession = filter_input(INPUT_GET, 'setSession', FILTER_SANITIZE_STRING);
if ($setSession == 'true') {
    $date = date("D M j G:i:s T Y");
    $_SESSION['newDate'] = $date;
    echo $_SESSION['newDate'];
}


<html>
<head>
</head>
<body>
  <button id="sessionTest" value="set new date">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script src="script.js"></script>
</body>
</html>


$('#sessionTest').on('submit', function () {
    $.get('setSession.php?setSession=true', function (data) {
        $('body').append('<p>' + data + '</p>');
    });
});