0

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

Tazz
  • 3
  • 1
  • 7
  • 3
    `$_SESSION['b']=$_GET['projet'];`: shouldn't that be `$_SESSION['b']=$_GET['word'];`? – PeterMader Jun 01 '17 at 15:05
  • oups, Yes it s" $_SESSION['b']", sorry, it's copy/past mistake – Tazz Jun 01 '17 at 15:06
  • I think you need to wrap `word` in quotes in order for it to work – Lixus Jun 01 '17 at 15:07
  • 1
    Any javascript errors? does `choice` want `var` in front of it? And no, it's too late to set the $_SESSION in the js. You'll need to do it async like in Sol#1. – mickmackusa Jun 01 '17 at 15:14
  • In sol1, I can send and echo the var "choice". I think, I need to send session variable with ajax ! – Tazz Jun 02 '17 at 10:52
  • Possible duplicate of [How to pass JavaScript variables to PHP?](https://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – Hamza Zafeer Jun 02 '17 at 11:05
  • I need to use the SESSION variable in every page so I can't use POST or GET method – Tazz Jun 02 '17 at 14:34

2 Answers2

1

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

Kyrylo Romantsov
  • 172
  • 1
  • 1
  • 11
0

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

JonLuca
  • 850
  • 6
  • 25