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

7
  • 3
    $_SESSION['b']=$_GET['projet'];: shouldn't that be $_SESSION['b']=$_GET['word'];? Commented Jun 1, 2017 at 15:05
  • oups, Yes it s" $_SESSION['b']", sorry, it's copy/past mistake Commented Jun 1, 2017 at 15:06
  • I think you need to wrap word in quotes in order for it to work Commented Jun 1, 2017 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. Commented Jun 1, 2017 at 15:14
  • In sol1, I can send and echo the var "choice". I think, I need to send session variable with ajax ! Commented Jun 2, 2017 at 10:52

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I don't want to use cookie ;) Is-it possible without it ?
0

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

2 Comments

oups, Yes it s" $_SESSION['b']", sorry, it's copy/past mistake
trying print()ing that variable before assigning it. Try $_GET["word"] as well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.