如何将 javascript 值插入到 php 会话变量中

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

How to insert javascript value into the php session variable

phpjavascriptsession-variables

提问by user123_456

I want to insert javascript value into the php session variable but inside the javascript function.

我想将 javascript 值插入到 php 会话变量中,但在 javascript 函数中。

Here is what I have tried and it is not working:

这是我尝试过但不起作用的方法:

function languageChange()
{
 var lang = $('#websites1 option:selected').val();
 <?php $_SESSION['SESS_LANGUAGE']?> = lang;
 alert(<?php echo $_SESSION['SESS_LANGUAGE']?>);
}

回答by Devner

You cannot access it directly (the way you are doing). However, it can be done using AJAX. Here is the perfectly working solution.

你不能直接访问它(你正在做的方式)。但是,它可以使用 AJAX 来完成。 这是完美的工作解决方案。

Here is the HTML:

这是 HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>1</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
    <label for="websites1">Websites</label>
    <select name="websites1" id="websites1">
        <option value="Design">Design</option>
        <option value="Dev">Dev</option>
        <option value="Ecom">Ecom</option>
    </select>
</form>
<script type="text/javascript" src="js/jquery_1.7.1_min.js"></script> 
<script type="text/javascript">

$(document).ready(function() {

    function languageChange()
    {
         var lang = $('#websites1 option:selected').val();

        return lang;
    }


    $('#websites1').change(function(e) {                    

        var lang = languageChange();

        var dataString = 'lang=' + lang;

        $.ajax({

            type: "POST",
            url: "pass_value.php",
            data: dataString,
            dataType: 'json',
            cache: false,
            success: function(response) {

                    alert(response.message);

                }
        });

        return false;

    });

});

</script>
</body>
</html>

Here is the AJAX part:

这是 AJAX 部分:

//pass_value.php

<?php session_start();

$_SESSION['SESS_LANGUAGE'] = $_POST['lang'];

$_SESSION['SESS_LANGUAGE'] = 'This is my PHP session var --->'.$_SESSION['SESS_LANGUAGE'];

print json_encode(array('message' => $_SESSION['SESS_LANGUAGE']));
die();


?>

EDIT 1:

编辑 1:

Once you run the code, simply select any of the other options from the dropdown menu and you will receive an alert that gives you the value of the php session variable along with the custom text that I added to it.

运行代码后,只需从下拉菜单中选择任何其他选项,您将收到一个警报,该警报为您提供 php 会话变量的值以及我添加到其中的自定义文本。

EDIT 2:

编辑2:

If you want to run my solution on your end, make sure you point to core jQuery js file correctly. My jquery code points to src="js/jquery_1.7.1_min.js". Make sure you update this.

如果您想最终运行我的解决方案,请确保正确指向核心 jQuery js 文件。我的 jquery 代码指向src="js/jquery_1.7.1_min.js". 确保你更新这个。

回答by DavidMB

Javascript does not have access to PHP-variables. The client (web browser/javascript) only has access to the Cookie or Cookieless ID (Querystring) associated with the session.

Javascript 无法访问 PHP 变量。客户端(网络浏览器/javascript)只能访问与会话关联的 Cookie 或 Cookieless ID(查询字符串)。

Sessions are used this way for the purpose of not letting the client modify settings associated with the session without going through the server. The less "secure" way is to use cookies for all settings. Something like language-setting might be a good idea to store in a cookie rather than the session.

以这种方式使用会话是为了不让客户端在不通过服务器的情况下修改与会话相关的设置。较不“安全”的方法是对所有设置使用 cookie。将语言设置之类的东西存储在 cookie 而不是会话中可能是个好主意。

In order to solve this, you could make a page which takes a session property name (like your "SESS_LANGUAGE") and a value which puts it in the session using php. I strongly advise against this because any user could then set any session variable.

为了解决这个问题,您可以创建一个页面,该页面采用会话属性名称(如您的“SESS_LANGUAGE”)和一个使用 php 将其放入会话的值。我强烈建议不要这样做,因为任何用户都可以设置任何会话变量。

The best way I can imagine is that you send an AJAX-call to a page saying that you want to change the language.

我能想象到的最好方法是您向一个页面发送一个 AJAX 调用,说您想要更改语言。

The called URL would be something like this: changelanguage.php?lang=en_US

被调用的 URL 将是这样的:changelanguage.php?lang=en_US

And the php-code for changelaguange.php: $_SESSION['SESS_LANGUAGE'] = $_GET['lang'];

和 changelaguange.php 的 php 代码: $_SESSION['SESS_LANGUAGE'] = $_GET['lang'];