首页 > 解决方案 > 会话文件未在 AJAX 的父页面上更新

问题描述

在 index.php 上,我正在运行以下 AJAX 来获取更新的通知:

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();

在 AJAX 调用的页面上,这里是代码......

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 );



}

如您所知,在 index.php 上的函数中,我正在提醒 number_of_previous_notifications 以查看它是否会发生变化。我让函数运行 4-5 次,并且 number_of_previous_notifications 永远不会更新。

这告诉我 session['number_of_notifications'] 没有在 Ajax 请求页面上更新。

有什么想法吗?

标签: javascriptphpjqueryajax

解决方案


old_notification_value好的...如果存储的变量 like高于其先前的值,我建议您尝试以下方法播放声音:

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.
}

您试图从$_session变量中处理它......但问题是,一旦 HTML 生成并发送到客户端,服务器端和客户端之间就没有通信。对$_session变量的更改不会反映在已经交付给客户端的网页中。

简而言之,您只能使用您拥有的 json 响应在客户端处理它。忘记$_SESSION那个任务。;)


推荐阅读