0

I am trying to update php SESSION variable in jquery mouse event but it is not updating. My jquery function is as follows:

$(document).mousemove(function(e){
        <?php $_SESSION['last_activity_time'] = time(); ?>
        alert('<?php echo date("H:i:s", $_SESSION['last_activity_time']); ?>')
}); 

I even used time() function in alert box to check and even with that every time I move the mouse i got the same time. The mousemove function is working since every time i moved it a alerbox shows. But every time the time is same. I don't know what I am doing wrong here...

In some post I found that I can use ajax for updating the session variable. But in that process , what i know(not an expert in ajax) is that the ajax call will goto other page and after performing desired function it return to the current page and I dont want to redirect to soem other php page. I want to update this session variable without moving to other page.

Kindly point out what i am doing wrong in above function and how i can solve this problem.

  • 1
    This is because the PHP runs on page load, the jQuery is running after page load (on mouse move). You would need to do a post request – Clint Jul 16 '20 at 18:23
  • 1
    Thanks for your comment. You are right after putting the question i did some more search and what i found is that the ajax is the only way. At least in my current knowledge. – akash yadav Jul 16 '20 at 18:29
  • For sure, you'll need to make an Ajax request to do this – Clint Jul 16 '20 at 18:30
  • but it arises some other problem..should it be okey to ask it here or I have to post other question?? – akash yadav Jul 16 '20 at 18:33
  • You should go and have a good, thorough read of https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming first of all. – CBroe Jul 17 '20 at 08:19
  • And then go inform yourself a bit about AJAX, because currently your perception of it seems to be far off. It is a _background_ request, which means there will be no “leaving” of the current page (unless you explicitly implemented something to do exactly that.) – CBroe Jul 17 '20 at 08:20

0 Answers0