Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

php - Set Session variable using javascript

   <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[].

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...