0

I have this working JS code in my encode.html file :

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
  <script type="text/javascript">
    var value; //this is the Value variable I want to access in PHP.
    value = 'some value here'; //value variable gets updated.
  </script>
</body>
</html>

And this working PHP code in sample.php file :

<?php
$jsValue = 'value variable from javascript should go here';
//I want to access the value variable (from javascript) in the above PHP variable.

echo $jsValue; //print the value
?>

And I want to merge both files, to one. And access the variable value from JavaScript and echo it in PHP, and use it in some functions later on. So, two things :

  • How should I correctly format the code (so I could see it all in one file)?
  • After merging the PHP & JS code, how should I access the value variable from JS in PHP, so I could assign it to a local PHP variable and use it later on?

I hope you understand my problem. Any help would be appreciated!

  • Your PHP code and your JavaScript code are separated by the Internet. – Pointy Oct 14 '20 at 13:27
  • What does this mean? – BigSantuary Oct 14 '20 at 13:28
  • 2
    Does this answer your question? [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) – Jeto Oct 14 '20 at 13:28
  • In general, it means you need to send the value of your js variable to your server (php code) and there to catch it and use it. – Angel Deykov Oct 14 '20 at 13:32
  • @Jeto So, is there **any** way I could pass a variable from client side to server side? – BigSantuary Oct 14 '20 at 13:40
  • [yes](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript), though please do try to search for answers first – loremdipso Oct 14 '20 at 13:48
  • @loremdipso Thanks! But the link you referred takes to a topic 'Pass PHP var to JS' - I am willing to the opposite... – BigSantuary Oct 14 '20 at 14:30
  • oh, my mistake. To do that you'll need to pass whatever variable up in your GET or POST request. – loremdipso Oct 14 '20 at 14:50

1 Answers1

0

My recommendation would be to save the value into a cookie. If you do this you can pull the value into PHP or into Javascript.

Javascript Code

<script type="text/javascript">
    
function setCookie(name,value,days) {
    var expires = "";
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days*24*60*60*1000));
            expires = "; expires=" + date.toUTCString();
        }
            document.cookie = name + "=" + (value || "")  + expires + "; path=/";
}
   
setCookie("shared_value", "some value here", 1);

</script>

PHP Code

<?php
/** check to see if shared_value cookie is found, then display */
echo (isset($_COOKIE['shared_value']) ? $_COOKIE['shared_value'] : 'value not set';
?>
meme
  • 11,861
  • 2
  • 19
  • 20