更新会话使用AJAX [英] Updating session using AJAX

查看:115
本文介绍了更新会话使用AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每当元素被点击的DIV会话进行更新,但它看起来像我的会议没有更新,每当我使用AJAX。我希望会话进行更新,而不仅仅是打印数据在我的AJAX。

这是我的ajax:

  $('。chat_div)。点击(函数(){
VAR ID = $(本).attr('身份证');
    $阿贾克斯({
        网址:'插件/ get_id.php',
        数据:{ID:ID},
        成功:函数(数据)
        {
            警报(< PHP的echo $ _SESSION ['chat_mate_id'];?>中);
        }
    });
    });
 

这是我的php文件:

 在session_start();
$ _SESSION ['chat_mate_id'] = $ _GET ['身份证'];
 

解决方案

正在生成一个HTML文件,其中有一些JS嵌入在里面。那时的你生成的HTML文档中的会话变量,你输出值到JS作为的字符串的。

接下来,运行JS,这使得第二个HTTP请求,并更新会话变量的值。

当响应到达时,提醒你你注入到页面的原来的页面加载时

字符串的值

如果你想作出反应的的会话变量的值,那么你必须使用你从服务器获取的更新的值数据。

运行PHP程序在一分钟前不会主动改变的输出。 Ajax不是时间旅行。

I want the session to be updated whenever an element is clicked in the div, but it looks like my session is not updating whenever I use an ajax. I want the session to be updated, not just printing the data in my ajax.

here is my ajax:

$('.chat_div').click(function(){
var id = $(this).attr('id');
    $.ajax({
        url: 'plugins/get_id.php',
        data: {id:id},
        success: function(data)
        {
            alert("<?php echo $_SESSION['chat_mate_id'];?>");
        }
    });
    });

here is my php file:

session_start();
$_SESSION['chat_mate_id'] = $_GET['id'];

解决方案

You are generating an HTML document, which has some embedded JS in it. At the time you generate the HTML document, you output the value of a session variable into the JS as a string literal.

Next, you run the JS, which makes a second HTTP request, and updates the value of the session variable.

When the response arrives, you alert the value of the string you injected into the page when the original page loaded.

If you want to react to the new value of the session variable, then you have to use data that you get from the server after you update the value.

The output of running a PHP program a minute ago will not change retroactively. Ajax is not time travel.

这篇关于更新会话使用AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆