javascript - 会话文件未在 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 请求页面上更新。
有什么想法吗?
解决方案
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
那个任务。;)
推荐阅读
- python - URL中的python增量编号
- python - 使用自己的新轴对相机进行 3D 旋转
- salesforce - 如何从新的自定义对象中删除字段?
- xcode - UI 问题 - Ionic - iphonex 顶部和底部的黑色空间问题
- elsa-workflows - elsa 工作流 blazor 集成的状态如何?
- sql - 在informix中如何将行转换为列
- c# - 如何将 EventRecord xml 转换为字典包括所有参数 - C#
- istio-gateway - 如何检索 HPE Ezmeral 容器平台托管集群的 istio 入口网关主机?
- c - 是否有任何技巧可以使用 unicode 符号在 c 中创建对称表
- javascript - 如何使用jQuery随时检测屏幕大小进行屏幕变化