简体   繁体   中英

Setting SESSION variable in PHP

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
}

How about you

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

And then

function displayNow(tag)
{
  $("#displayLater span").html(tag);
}
$("#page3contents").load("page3.php",{tagger:x});

Will send variables to POST method. So you'll have to change the line in page3.php to :

$tag=$_POST['tagger'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM