donglu3184 2013-08-22 22:22
浏览 62
已采纳

在PHP中设置SESSION变量

Page1.php has a variable "flag" with value=1. When clicked, the javascript function "ajaxreq()" is called, and a text "Click me" appears below (ajax request from page2.php).

When clicked upon the appeared "click me", it calls "ajaxreq2(3)", and displays the text message "success". On clicking upon the displayed text message "success", the div content( id : displayLater) is set to display, which was hidden till now. This div has to contain the updated value of $_SESSION['tag'] in the $flag variable. How can i update it?

In this code, it gives out error : "undefined index 'tag' in page1.php".

In order to make use of the variable $_SESSION['tag'], I have to reload page1.php everytime. Otherwise, it remains uninitialized. Is there a possibility that I can use the updated variable in page1.php?

page1.php

<?php
session_start();
$flag=1;
echo "<span onclick='ajaxreq()'>$flag</span>";
?>

<div id="page2contents"></div>

<div id="displayLater" style="display:none">
  <span>This is done.</span>
  <?php
   if(isset($_SESSION['tag']))
     $flag=$_SESSION['tag'];
  ?>
  <span onclick="displayNow($flag)">$flag</span>
</div>

page2.php

<?php
echo "<div onclick='ajaxreq2(3)'>Click me</div>";
?>
<div id="page3contents.php"></div>

page3.php

<?php
session_start();
$tag=$_POST['tagger'];
$_SESSION['tag']=$tag;
echo "<span onclick='displayNow($tag)'>success</span>";
?>

JAVASCRIPT functions are as follows:

function ajaxreq()
{
  $("#page2contents").load("page2.php");
}

function ajaxreq2(x)
{
  $("#page3contents").load("page3.php",{tagger:x});
}

function displayNow(abc)
{
  abc=abc+2;    //some operation on abc
  $("#somethingToDisplay").show();   //displaying something else
}
  • 写回答

2条回答 默认 最新

  • dongle7882 2013-08-22 22:35
    关注

    How about you

    echo "<span onclick='displayNow('".$_SESSION['tag']."')'>success</span>";
    

    And then

    function displayNow(tag)
    {
      $("#displayLater span").html(tag);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败