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

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Nimesh Vagadiya
  • 602
  • 3
  • 8
  • 18
  • 3
    1. welcome to SO. 2. ignore the downvoters that do not comment. 3. please, explain your question more in detail. What do you want to achieve? what does "it does not work" mean really? – Salvatorelab Feb 25 '13 at 12:50
  • i dont see connection between your code and setting session? have you look at php session tutorials? One is [here](http://www.w3schools.com/php/php_sessions.asp) – Bojan Kovacevic Feb 25 '13 at 12:51
  • wait,you want to acccess to php session from javascript? – Bojan Kovacevic Feb 25 '13 at 12:53
  • who are you to give me down votes ? @TheBronx – Nimesh Vagadiya Feb 25 '13 at 12:55
  • I've not downvoted the question. Downvoting without commenting is just trolling. And downvoting newbies without commenting is doubletrolling :P But please, take your time to write the question properly, read the comments and edit your question to add more detail. – Salvatorelab Feb 25 '13 at 13:03

1 Answers1

6

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'];
Jitesh Tukadiya
  • 1,269
  • 1
  • 11
  • 25
  • 2
    Probably an obvious thing to point out, but in case any reads this and doesn't realise - if you need to store anything that people shouldn't be changing, you need to add additional security and/or validation. For instance, don't use this for logging in a user and setting their UserId in the session! Anyone could spoof a call to your PHP script, it's not only your page's javascript that can access it. – almcnicoll Apr 11 '14 at 13:37
  • I guess it's not too bad if "value1" is hardcoded like he did, and if value1 is not really all that important... But yeah, NEVER, EVER do somehting like $_SESSION[$_POST['variableName']] = $_POST['variableValue'];, that would let anybody set any variable to any value they want :O – Mathieu Turcotte Jan 05 '17 at 18:51