简体   繁体   English

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

[英]How to insert javascript value into the php session variable

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']?>);
}

You cannot access it directly (the way you are doing). 您无法直接访问它(操作方式)。 However, it can be done using AJAX. 但是,可以使用AJAX完成。 Here is the perfectly working solution. 这是完美的解决方案。

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. 如果要最终运行我的解决方案,请确保正确指向核心jQuery js文件。 My jquery code points to src="js/jquery_1.7.1_min.js" . 我的jquery代码指向src="js/jquery_1.7.1_min.js" Make sure you update this. 确保您对此进行了更新。

Javascript does not have access to PHP-variables. Javascript无法访问PHP变量。 The client (web browser/javascript) only has access to the Cookie or Cookieless ID (Querystring) associated with the session. 客户端(Web浏览器/ 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. 不太“安全”的方法是对所有设置使用cookie。 Something like language-setting might be a good idea to store in a cookie rather than the session. 诸如语言设置之类的东西可能是存储在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. 为了解决这个问题,您可以制作一个页面,该页面带有会话属性名称(例如您的“ SESS_LANGUAGE”)和一个使用php将其放入会话的值。 I strongly advise against this because any user could then set any session variable. 我强烈建议您这样做,因为任何用户都可以设置任何会话变量。

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 = zh_CN

And the php-code for changelaguange.php: $_SESSION['SESS_LANGUAGE'] = $_GET['lang']; 以及changelaguange.php的php代码:$ _SESSION ['SESS_LANGUAGE'] = $ _GET ['lang'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM