How to assign JavaScript variable to PHP session variable?

前端 未结 2 1701
迷失自我
迷失自我 2021-01-27 11:55

Here\'s javascript code that i have

  var randomnum = 30;

and here\'s PHP code



        
相关标签:
2条回答
  • 2021-01-27 11:59

    You can't access Session directly in JavaScript.

    You can make a hidden field and pass it to your page and then use JavaScript to retrieve the object via document.getElementById

    0 讨论(0)
  • 2021-01-27 12:25

    Look at the following code. The PHP session is assigned to 30 from the Javascript value. however am not sure if this is good way for implementation.

    <?php
    // Start the session
    session_start();
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Html Page Title</title>
    </head>
    
    <body>
    
        <script>
            var randomnum = 30;
         </script>
    
        <?php $_SESSION['numbertoguess'] = '<script>document.write(randomnum)</script>';?>
        <?php echo $_SESSION['numbertoguess']; ?>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题