Session file not being updated on parent page of AJAX

问题: On index.php, I am running the following AJAX to get updated notifications: function updateNotifications() { var user_id = "<?php echo $user_id; ?>"; var p...

问题:

On index.php, I am running the following AJAX to get updated notifications:

function updateNotifications() {

    var user_id = "<?php echo $user_id; ?>";
    var phpSessionId = "<?php echo session_id(); ?>";   
    var number_of_previous_notifications = "<?php echo $_SESSION['number_of_notifications']; ?>";

alert(number_of_previous_notifications);

  $.ajax({
      url : "notification_refresh.php",
      type : "POST",
      data : { "user_id": user_id, "session_id": phpSessionId },
      success : 
       function(data) {

         var response = $.parseJSON(data);

         if (response.messages > 0) {
             $('.inactive-notification-img').hide();
             $('.notification-img').show()
             $('.notification-number').html(response.messages).addClass('blink_me');


         }
         else {
                  $('.notification-img').hide();
              $('.inactive-notification-img').show();
              $('.notification-number').html('').removeClass('blink_me');
         }
     }  

   });
   setTimeout('updateNotifications()', 15000); // Every 15 seconds.


}

    updateNotifications();

On the page that AJAX calls, here is the code...

if (isset($_POST['session_id'])) { session_id($_POST['session_id']); }
session_start();

include_once('../../inc/database.php');
include_once('../../inc/session.php');


if (
(isset($_POST['user_id'])) && (is_numeric($_POST['user_id'])) 
){


$notif_user_id = $_POST['user_id'];



         $search_notif_query = $db1q->query("SELECT id FROM User_Notifications WHERE FIND_IN_SET('$notif_user_id', to_user) AND status = '0'");

         $response = ['messages' => $search_notif_query->num_rows];

         $_SESSION['number_of_notifications'] = $search_notif_query->num_rows;


         echo json_encode( $response );



}

As you can tell, in the function on index.php, I am alerting the number_of_previous_notifications to see if it ever changes. And I let the function run 4-5 times and the number_of_previous_notifications is NEVER updated.

Which is telling me that the session[‘number_of_notifications’] is not being updated on the Ajax request page.

Any thoughts?


回答1:

Okay... Here is what I suggest you to try in order to play a sound if a stored variable like old_notification_value is higher than its previous value:

var old_notification_value = 0;  // A variable to store the notification amount over time

function updateNotifications() {

  var user_id = "<?php echo $user_id; ?>";
  var phpSessionId = "<?php echo session_id(); ?>";
  var number_of_previous_notifications = "<?php echo $_SESSION['number_of_notifications']; ?>";

  alert(number_of_previous_notifications);

  $.ajax({
    url : "notification_refresh.php",
    type : "POST",
    data : { "user_id": user_id, "session_id": phpSessionId },
    success :
    function(data) {

      var response = $.parseJSON(data);

      if (response.messages > 0) {
        $('.inactive-notification-img').hide();
        $('.notification-img').show()
        $('.notification-number').html(response.messages).addClass('blink_me');
      }
      else {
        $('.notification-img').hide();
        $('.inactive-notification-img').show();
        $('.notification-number').html('').removeClass('blink_me');
      }

      if(response.messages > old_notification_value){   // Here you handle the sound notification.
        // Update the old_notification_value variable!
        old_notification_value = response.messages;

        // Then play a sound here!
      }

    }

  });
  setTimeout('updateNotifications()', 15000); // Every 15 seconds.
}

You tried to handle it from the $_session variable... But the thing is that there is no communication between server-side and client-side once the HTML has been produced and sent to the client. Changes on a $_session variable just won't reflect in the web page already delivered to the client.

In short, you can handle that on client-side only with the json response you have. Forget about $_SESSION for that task. ;)

  • 发表于 2019-03-08 20:50
  • 阅读 ( 202 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除