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 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?