Tazz
Tazz

Reputation: 3

Pass JS var to PHP var

I need to have a "global" variable because I need to use it in different page and I want to modify it too: I think that I need to use $_SESSION

I need to change this variable, when the user click on dropdown or list.

I have this:

SOLUTION 1

PageA:

$('#list.test li').on('click',function(){           
    choice=$(this).attr('id');
    $.ajax({
      url: "PageB.php",
      data: {word : choice},
      dataType: "html",
      success: function (data) {
      $('#content_table').html(data);                                 
      }
  });                                   
});

PageB

session_start();
$_SESSION['b']=$_GET['word'];
echo $_SESSION['b']; // It works

PageC for verify the result

session_start();
echo $_SESSION['b']; // Error !!

In my PageC, I have an error ( Notice: Undefined index: b )

Is it possible to update session variable with ajax ?

SOLUTION 2

PageA: I want to passe the id JS var to PHP var

$('#list.test li').on('click',function(){           
    choice=$(this).attr('id');
    <?php $_SESSION['b'] ?> = choice; //<--- it is possible ?
    $.ajax({
      url: "PageB.php",
      data: {word : choice},
      dataType: "html",
      success: function (data) {
      $('#content_table').html(data);                                 
      }
  });                                   
});

This solution doesn't work because AJAX and PHP are note in the same side (client/server).

Thank you

Upvotes: 0

Views: 197

Answers (2)

Kyrylo Romantsov
Kyrylo Romantsov

Reputation: 182

You can push data to cookies via JavaScript, smth like document.cookie = "key=value";
And receive it on back-end like $_COOKIE["key"];.

Upvotes: 1

JonLuca
JonLuca

Reputation: 919

$_SESSION['b']=$_GET['projet']; should be $_SESSION['b']=$_GET['word'];

Upvotes: 0

Related Questions