如何使用 JavaScript 设置会话变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34199406/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:22:32  来源:igfitidea点击:

How can I set session variable with JavaScript

javascriptphpjquerysession

提问by shopeeon

By using below code on button click I get the href attribute value, until this everything is fine but when I got my hreflink on button click we want to store it in the session variable (i.e. var href) because we also use this variable elsewhere. Please suggest me how to store JavaScript variable in session.?

通过在按钮单击时使用以下代码,我获得了 href 属性值,直到一切正常,但是当我href在按钮单击时获得链接时,我们希望将其存储在会话变量(即 var href)中,因为我们还在其他地方使用了该变量。请建议我如何在会话中存储 JavaScript 变量。?

<script>
        $(document).ready(function(){
            $('.button').click(function(){
                var href = $(this).val();
                alert(href);
            })
        });
    </script>
<button value="<?php echo $value['3']; ?>" class="button" style="background-color:#ff8c21;">Buy Now</button>

回答by Anand Singh

You can't set SESSION WITH JAVASCRIPTthis is because its server side functionality so if you want to use this datasome where else you can send a ajax request to server and there you can create session for this data.

你不能用JAVASCRIPT设置SESSION这是因为它的服务器端功能所以如果你想data在其他地方使用它你可以向服务器发送 ajax 请求,在那里你可以为这个数据创建会话。

JS:

JS:

$(document).ready(function(){
            $('.button').click(function(){
                var href = $(this).val();
                $.ajax({
                    type: "POST",
                    url: url,//url to file
                    data: {val:href},
                    success: function(data){
                    //success
                      };

                    });
                 });
              });

PHP:

PHP:

 session_start();
 if(isset($_POST['val']))
 {
  $_SESSION['href'] = $_POST['val'];//created a session named "href"
 }
//use session 
if(isset($_SESSION['href']))
{
echo $_SESSION['href'];//use your session.
}

回答by Digpal Singh

Please try it if you want to use session using jquery First include jquery-1.9.1.js and jquery.session.js

如果您想使用 jquery 使用 session 请尝试一下首先包含 jquery-1.9.1.js 和 jquery.session.js

$(document).ready(function(){ $('.button').click(function(){var href = $(this).val();$.session.set("yoursessioname", "storevalue");}) });
alert($.session.get("yoursessioname"));

more detail http://phprocks.letsnurture.com/create-session-with-jquery/

更多细节http://phprocks.letsnurture.com/create-session-with-jquery/

回答by gavin

JavaScript works in the browser and PHP sessions are activated on the server, so you can't change session variables without sending a request to the server eg via a url or by submitting a form. BUT you could use sessionStorage, which is a similar principle but lives in the browser and is modifiable by javascript.

JavaScript 在浏览器中运行,PHP 会话在服务器上激活,因此您不能在不向服务器发送请求(例如通过 URL 或提交表单)的情况下更改会话变量。但是你可以使用 sessionStorage,这是一个类似的原理,但存在于浏览器中并且可以通过 javascript 修改。

eg. (inside javascript)

例如。(在 JavaScript 中)

sessionStorage.myvariable=myVal

etc. It will live as long as your tab is open (from page to page).

等等。只要您的选项卡处于打开状态(从页面到页面),它就会一直存在。

回答by Robert

If you want to store it in session which is SERVER side then you need use ajax pass variable and store it in php for example with

如果要将其存储在 SERVER 端的会话中,则需要使用 ajax 传递变量并将其存储在 php 中,例如

 <?php
 session_start();
 !isset($_POST['var']) ?: $_SESSION['var'] = $_POST['var'];

but you can think of using cookies as well, then you don't need PHP it can be done only with JavaScript. There is nice Jquery pluginfor that

但是你也可以考虑使用cookies,那么你不需要PHP它只能用JavaScript来完成。有不错的jQuery插件