如何将javascript值插入到php会话变量中 [英] How to insert javascript value into the php session variable

查看:129
本文介绍了如何将javascript值插入到php会话变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我尝试过的,但它不起作用:

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


推荐答案

您无法直接访问它(你的方式)。但是,它可以使用AJAX完成。
这是一个完美的解决方案。

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

这是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>

以下是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();


?>

编辑1:

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

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.

编辑2:

如果您想在最后运行我的解决方案,请确定你正确指向核心jQuery js文件。我的jquery代码指向 src =js / jquery_1.7.1_min.js。请务必更新。

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.

这篇关于如何将javascript值插入到php会话变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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