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 ; ?>

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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

        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) {


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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply