Set Session variable using javascript

血红的双手。 提交于 2019-12-28 06:51:33

问题


   <script type="text/javascript">
    function checkQuery()
    {
      var val = form1.proDown.options[form1.proDown.options.selectedIndex].value;
      var txt = form1.proDown.options[form1.proDown.options.selectedIndex].text;
      //alert(val+' | '+txt);
      <?php $_SESSION['value1']= ?> = txt; <?php ; ?>
     }
   </script>

I have this code and it does not Work? Any One have solution for accessing javascript variable into $_SESSION[].


回答1:


I think you should use xhr(Ajax) to store your data into php session. Following is a simple example to do this

    jQuery.ajax({
        url: 'storesession.php',
        type: 'POST',
        data: {
            txt: txt,
        },
        dataType : 'json',
        success: function(data, textStatus, xhr) {
            console.log(data); // do with data e.g success message
        },
        error: function(xhr, textStatus, errorThrown) {
            console.log(textStatus.reponseText);
        }
    });

storesession.php

$_SESSION['value1'] = $_POST['txt'];


来源:https://stackoverflow.com/questions/15067335/set-session-variable-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!