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条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化