从JS内设置PHP SESSION var [英] Setting PHP SESSION var from within JS

查看:279
本文介绍了从JS内设置PHP SESSION var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过JS函数设置会话变量?

Is there a way to set a session variable from withing my JS function?

我有以下JS代码:

$.ajax({
    url: "ajax.php",
    type: "POST",
    data: { 
        tid: '.$testID.',
        do:"'.$do.'"
    },
    success: function( html ) {
        $("#partBox").html( html ); 
        // add PHP here?            
    }
});

我想在会话中成功设置以下变量:

I would like to set the following var in the session, on SUCCESS:

$_SESSION['hgt'] = 'Math.ceil($("#partBox").height() / 2)';

我什至不知道这是否有可能...或者,我可能会使用cookie ...

I have no idea if this is even possible... Alternatively I could probably use cookies...

推荐答案

您无法在JavaScript中设置任何PHP变量. Page完成加载后,PHP便完成了它的工作,并且超出了画面.如果要使用JavaScript设置会话变量,则可以使用以下两种方法之一:

You can't set any PHP variables in JavaScript. Once the Page has finished loading PHP has done its job and is out of the picture. If you want to set a session variable using JavaScript you can do it one of two ways:

  1. 使用Ajax将值发送到PHP脚本以设置会话变量
  2. 将其存储在cookie中,并在下一页加载时让PHP使用该cookie的值来创建会话变量

这篇关于从JS内设置PHP SESSION var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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