Reputation: 163
I want the session to be updated whenever an element is clicked in the div, but it looks like my session is not updating whenever I use an ajax. I want the session to be updated, not just printing the data in my ajax.
here is my ajax:
$('.chat_div').click(function(){
var id = $(this).attr('id');
$.ajax({
url: 'plugins/get_id.php',
data: {id:id},
success: function(data)
{
alert("<?php echo $_SESSION['chat_mate_id'];?>");
}
});
});
here is my php file:
session_start();
$_SESSION['chat_mate_id'] = $_GET['id'];
Upvotes: 0
Views: 150
Reputation: 943564
You are generating an HTML document, which has some embedded JS in it. At the time you generate the HTML document, you output the value of a session variable into the JS as a string literal.
Next, you run the JS, which makes a second HTTP request, and updates the value of the session variable.
When the response arrives, you alert the value of the string you injected into the page when the original page loaded.
If you want to react to the new value of the session variable, then you have to use data that you get from the server after you update the value.
The output of running a PHP program a minute ago will not change retroactively. Ajax is not time travel.
Upvotes: 4