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

报告相同问题?