duankui1532 2012-07-18 11:12
浏览 93
已采纳

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

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']?>);
}
  • 写回答

2条回答 默认 最新

  • dongmi5020 2012-07-18 12:01
    关注

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

    Here is the 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:

    //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:

    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.

    EDIT 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路