dtr32221 2014-03-28 21:57
浏览 52
已采纳

从页面传递到另一个页面的变量值保持不变

What i am trying to do is to pass a value, using a variable, from one page using php to another page using php and then pass this value in the second page from php to javascript.

I initialize the variable in HTML depending on which image i click. The problem is that every time i pass a different value(different values are 12 and 7 in the code below), meaning that i clicked on a different image,and after reloading my page, i still get the first value.

I tried unsetting the variable and/or session_destroy() but with no luck. To be more clear:

First page:

    <?php
    session_start();
    ?>

    <!DOCTYPE HTML>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <a href="secondpage.php"><img src="images/first_image.png" alt="image" <?php $_SESSION['choice'] = 12; ?>></a>
    <a href="secondpage.php"><img src="images/second_image.png" alt="image" <?php $_SESSION['choice'] = 7; ?>></a>
    </body>

</html>

Second page:

<?php
session_start();
$option = $_SESSION['choice'];
//unset($_SESSION['choice']); //failed
echo $option;                 //option is always 12
//session_destroy();         //failed

?>
<!doctype html>
<html>
<head>
<title>Monuments</title>

<script>
var option = <?php echo json_encode($option); ?>;
//var option = "<?php echo $option; ?>";      // i tried also this way 
alert("option is : " + option);  //option is always 12
</script>

</head>

<body>

</body>
</html>

Can you tell me what is the problem? Or recommend any better solutions for what i want to do?

  • 写回答

4条回答 默认 最新

  • dtef9322 2014-03-28 22:00
    关注

    You are assigning values to $_SESSION['choice'] in your HTML.

    Using sessions is not really the proper way to do what you are trying to achieve.

    On your first page, use a $_GET parameter to identify the selected monument.

    <a href="secondpage.php?choice=12"><img src="images/first_image.png" alt="image"></a>
    

    On your second page, you can get this parameter by using $_GET['choice']

    The code with the changes:

    <!DOCTYPE HTML>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <a href="secondpage.php?choice=12"><img src="images/first_image.png" alt="image"></a>
        <a href="secondpage.php?choice=7"><img src="images/second_image.png" alt="image"></a>
    </body>
    
    </html>
    

    And the second page:

    <?php
    $option = $_GET['choice'];
    echo $option;
    ?>
    
    <html>
    <head>
    
        <title>Monuments</title>
    
        <script>
        var option = <?php echo $option; ?>;
        alert("option is: " + option);
        </script>
    
    </head>
    
    <body>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大