如何将 JavaScript 变量分配给 PHP 会话变量? [英] How to assign JavaScript variable to PHP session variable?

查看:40
本文介绍了如何将 JavaScript 变量分配给 PHP 会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 javascript 代码

Here's javascript code that i have

  var randomnum = 30;

这里是 PHP 代码

<?php $_SESSION['numbertoguess'] = '<script>document.write(randomnum)</script>';?>

但这不是传递值但是当我在下面尝试此代码时,它可以工作.它为会话变量提供了值示例事物".

But this is not passing the value But when i am trying this code below, it works. It gives session variable that the value 'a sample thing'.

<?php $_SESSION['numbertoguess'] = 'a sample thing';?>

推荐答案

看下面的代码.PHP 会话从 Javascript 值指定为 30.但是我不确定这是否是实施的好方法.

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>

这篇关于如何将 JavaScript 变量分配给 PHP 会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆