0

In a java function I want to pass a variable over to $_SESSION. I have tried to do this like the following ... $_SESSION['ID'] = document.getElementById('lbxDates').value;

This does not work for me, so if anyone knows how i should approach this in a proper manner any help would be greatly appreciated!

<script type="text/javascript">
function GetID(){$_SESSION['ID']=document.getElementById('lbxDates').value;} 
</script>
Mark
  • 85
  • 9
  • 1
    You can't. By the time the JS gets to the browser to execute, the PHP has already executed on the server. – AbraCadaver Apr 10 '19 at 18:09
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Dave Apr 10 '19 at 18:16
  • If your session data is not secret, manage it with javascript... https://www.w3schools.com/jsref/prop_win_sessionstorage.asp – Fellipe Sanches Apr 10 '19 at 19:47

1 Answers1

0

The interaction between Javascript and PHP is one way, meaning you can do this:

document.getElementById('lbxDates').value = <?= $_SESSION['ID'] ?>;

but not this:

$_SESSION['ID'] = document.getElementById('lbxDates').value;

To accomplish what you're looking for, you'll need to make use of XMLHTTP requests (or if you use JQuery, AJAX). Here's an example of someone accomplishing this: Can i set session variables when posting data via javascript?

Daniel G
  • 539
  • 4
  • 10
  • Ok understood, I will try this out from the link and see how i get on thank you very much! – Mark Apr 10 '19 at 18:16