3

Consider I've this page: sample.php

<div id="id1" style="display: none">
Hello
</div>

Initially when the page loads, this div is not displayed, but I've a button1. When I click on this button, it changes the display value to block.

There's another button2 that I use to change the display value back to none.

So, this div is visible only when the button1 is clicked. In other cases (when the page is reloaded or when button2 is clicked), it would be invisible.

Now, suppose that I've opened this same page in 4 more tabs (it can be even more, I'm giving just example) of a browser. And I've pressed button1 in any 2 of them. That means this div is visible in those 2 tabs.

Now, what I want is that when the div is visible, the text inside it should store in a SESSION variable. And I can do it using JavaScript, jQuery, Ajax and PHP. When the two tabs have 2 visible divs, their text "Hello" is stored in a same SESSION variable (it will store only "Hello" only, not "HelloHello" since SESSION will get replaced by new request as my Ajax and PHP code will run on all tabs at the same time).

Now what I finally need is that when those 2 visible divs in 2 differents tabs are set to display: none, it should unset the SESSION variable that has "Hello" in it.

Because my condition for unsetting it is that none of the 4 tabs should have that div visible. If any of the tab has div visible, the SESSION would have "Hello" in it.

PS: I've tried to explain a lot my problem, but still if you don't understand, please mention it in comment, before answering:

Thank you.

Vikas Kumar
  • 689
  • 3
  • 7
  • 18
  • 1
    In short you are using the session variable to check if the div is displayed right? – Anoop saju Apr 17 '16 at 04:59
  • See http://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload/30144363#30144363 , http://stackoverflow.com/questions/36146595/can-we-refer-to-javascript-variables-across-webpages-in-a-browser-session/36146764#36146764 , http://stackoverflow.com/questions/36190731/cleartimeout-set-in-another-tab/36191942#36191942 – guest271314 Apr 17 '16 at 05:12
  • @Anoopsaju, no, I'm using JavaScript to check the visibility, and if it is visible, I'm storing the text inside into the SESSION variable. – Vikas Kumar Apr 17 '16 at 05:16
  • Instead of checking the visibility,you can store the text into the session variable when the button1 is clicked – Anoop saju Apr 17 '16 at 05:25
  • I want to store it only when it is visible, and delete it when it's not visible in all tabs. – Vikas Kumar Apr 17 '16 at 05:31

1 Answers1

1

In this code ,when button1 or show button is clicked,it will send the data to new.php.

The new.php will set the session variable.I have use another session variable count for taking the count of the number of tabs in which it is visible.

When the hide button is clicked, a post request is sent and the session variable count is reduced.When the count variable is reduced to 0 , there are no more tabs in which it is visible.So the session is destroyed

Here is the code:

HTML:

 <html>
 <head>
 <script src="jquery/jquery-1.12.2.min.js"></script>
 <script>
 $(document).ready(function(){

 $("#show").click(function(){
   $("#id1").css("display","block");
   var x=$("#id1").text();
   $.post("new.php",{val: x});
 });

 $("#hide").click(function(){
   $("#id1").css("display","none");
   $.post("new.php");

});
 });
 </script>
 </head>
<body>
  <div id="id1" style="display: none">
  Hello
  </div>
 <button type="button" id="show">show</button>
 <button type="button" id="hide">Hide</button>

 </body>
  </html>

new.php:

<?php
session_start();

if(!empty($_POST["val"]))
{
  if(!isset($_SESSION["val"]))
  {
    $_SESSION["val"]=$_POST["val"];
    $_SESSION["count"]=1;
   }
  else {
    $_SESSION["count"]+=1;
  }
}
else
{
  if(isset($_SESSION["val"]))
  {
    if($_SESSION["count"]==1)
    {
      session_destroy();
    }
   else {
     $_SESSION["count"]-=1;

    }
 }
 }


 ?>
Anoop saju
  • 480
  • 3
  • 17