jQuery单击事件来更改php会话变量 [英] jQuery click event to change php session variable

查看:50
本文介绍了jQuery单击事件来更改php会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好的方法是什么?

因为,正如我发现的那样(并且它完全有道理,只有在尝试之后:p)你可以在javascripts条件下设置一个PHP变量。 (duurrhh)

Because, as I found (and it made total sense, only after having tried it :p) that you can't set a PHP variable on javascripts conditions. (duurrhh)

我能想到的唯一解决方案是对处理会话变量的小PHP文件进行AJAX调用

The only solution I can come up with is to do an AJAX call to a small PHP file that handles the session variables

elm.click(function() {
   $.post("session.php", { "hints":"off" });
   turnOffHints();
}

然后让 session.php 处理设置新变量。

And then let the session.php deal with setting the new variable.

但是它只是为了设置一个完整的http请求而感到浪费PHP中的变量...

But it sorta feels like a waste to do a full http request only to set one variable in PHP...

推荐答案

Javascript (jQuery)在客户端执行。

Javascript (jQuery) is executed on the client-side.

PHP及其会话机制,在服务器端执行。

PHP, and its session mecanism, is executed on the server-side.

所以,做任何事情与PHP会话相关,你必须去服务器,做一个完整的HTTP请求 - 你不能留在客户端。

So, to do anything related to a PHP session, you have to go to the server, doing a full HTTP Request -- you cannot stay on the client-side.

做一个Ajax请求是一种去服务器的方式 - 工作正常;-)

你正在做的是OK (和ne实际上是cessary),如果你想影响一些会话数据:你没有太多的选择,即使它感觉像某种浪费。

Doing an Ajax request is a way of going to the server -- works fine ;-)
What you are doing is OK (and necessary, actually), if you want to affect some session data : you don't have much of a choice, even if it feels like some kind of waste.

这篇关于jQuery单击事件来更改php会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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