使用 onclick 使用 jquery 或 javascript 创建会话变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8085614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use onclick to create session variable with jquery or javascript?
提问by BerkErarslan
Is it possible onclick to create session variable with javascript or jquery then use this variable with php ?
是否可以使用 javascript 或 jquery onclick 创建会话变量,然后将此变量与 php 一起使用?
For example someone click link at the bottom
例如有人点击底部的链接
<div id="list"><a onclick = "" href="javascript:void(0);">+</a></div>
there will be created variable like number=1;
将创建变量,如 number=1;
then I should call it in php like :
那么我应该在 php 中调用它,例如:
<?php $_SESSION["number"]; ?>
回答by GEMI
In order to pass a variable from JavaScript to PHP you have to make an ajax call. The easiest way is to use Jquery for that:
为了将变量从 JavaScript 传递到 PHP,您必须进行 ajax 调用。最简单的方法是使用 Jquery:
<script type="text/javascript">
function submitinfo(){
$.ajax({
type: "POST",
url: "yourfile.php",
data: "values=" + $("#button1");
});
}
</script>
Also add a function to a button or something...
还要为按钮或其他东西添加一个功能......
<input type="button" id="button1" value="<?php print $values;>" onclick="submitinfo();"/>
Then in yourfile.php you simply get those values from $_POST['values'] and use wherever you want.
然后在 yourfile.php 中,您只需从 $_POST['values'] 获取这些值并在您想要的任何地方使用。
As others have pointed out you should not be creating session parameters with JavaScript, however if you need to get something from the client side to your server side, Ajax calls is the way to do it.
正如其他人所指出的,您不应该使用 JavaScript 创建会话参数,但是如果您需要从客户端到服务器端获取某些内容,Ajax 调用是实现此目的的方法。
回答by xgencoder
Sessions are maintained server side, it is not recommended to create sessions client side, if it is very much necessary, you can call a server side script from client using any asynchronous libraries and have the server side code create session for you...
会话在服务器端维护,不建议在客户端创建会话,如果非常有必要,您可以使用任何异步库从客户端调用服务器端脚本,并让服务器端代码为您创建会话...