John Davidson

javascript - AJAX request not getting updated values of $_SESSION variable

0 comments
Message:


I'm currently developing a web application and I am using PHP 8.0.3.


For some reason, the Ajax request is not capable of getting the updated value of the SESSION variable unless I forcefully refresh the page. I am calling the ajax function first when the page finishes loading, and then every minute.


I am using AJAX to update the values on a CanvasJS graph.


Can someone help me identify what am I doing wrong?


First, I start the session on a global file that I require on the scripts.


session_name("mfm");
session_start();

Snippet of my current ajax request on index.php page:


$.ajax({
method: "POST",
url: "php_func/somefile.php",
success: function(data){
console.log(data);
var dbdata = JSON.parse(data);
var qtyNuts = [];
var time = [];
dbdata = dbdata.reverse();
for (i = 0; i < dbdata.length; i++) {
time[i] = dbdata[i][1];
qtyNuts[i] = dbdata[i][0];


}
...

And this is part of the php_func/some_file.php:


if(isset($_SESSION['bar']))
{
$processed = $_SESSION['bar'];

$max = count($processed);
if($max > 5)
{
$max = 5;
}
$recent_data = array_slice($processed, -$max);

foreach($recent_data as $data)
{
$kgs = $data[0] / 120.0;

array_push($kgdata, array($kgs, $data[1]));
}

echo json_encode(array_reverse($kgdata));
}

I typed /php_func/some_file.php on my localhost URL and it works, kgdata array is getting updated as the SESSION variable updates.


And this is a snipped of where the session variable is created (this script runs as soon as the page is loaded and then every minute). Update.php:


$bar = array()
if(isset($_SESSION['bar'])
{
$bar = $_SESSION['bar'];
}
$b = array(count($buffer), date("H:i:s"));
array_push($bar, $b);
$_SESSION['bar'] = $bar;

For some reason, the ajax request does not update the values in the index.php graph, unless I forcefully refresh the page. What am I doing wrong?


I tried



  • Checking session id. They are the same in all files.

  • Use $.post instead. I have the same issue.

  • Change the setInterval for the ajax function to be more than a minute in case there is clashing between update.php and somefile.php



My Answer:
"my_session");
session_start();

Then, in my PHP file where I update the SESSION variable, I have something like this:

$_SESSION['my_variable'] = $new_value;

And in my JavaScript file, I have the following AJAX request:

function updateGraph() {
$.ajax({
url: 'update_session.php',
type: 'POST',
success: function(data) {
// Update graph with new data
}
});
}

$(document).ready(function() {
updateGraph();
setInterval(updateGraph, 60000);
});

In the update_session.php file, I have the following code:

session_start();
echo $_SESSION['my_variable'];

The issue you are facing might be due to the fact that the session data is not being updated in the AJAX request because the session is not being properly initialized or updated in the PHP file that handles the AJAX request.

To ensure that the session data is updated in the AJAX request, you should make sure to call session_start() at the beginning of the PHP file that handles the AJAX request. This will ensure that the session data is properly initialized and updated before the AJAX request is processed.

Additionally, you should also make sure that the session data is being properly updated in the PHP file that updates the SESSION variable. Make sure that you are setting the new value of the SESSION variable correctly and that the session is being properly saved after the update.

By ensuring that the session is properly initialized and updated in both the PHP file that updates the SESSION variable and the PHP file that handles the AJAX request, you should be able to get the updated value of the SESSION variable in the AJAX request without having to forcefully refresh the page.

Rate this post

5 of 5 based on 6686 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us