1

Ok, so I have a counter which simply counts up a second at a time.This second counter I want it to be my session variable.Is this a bad idea? Also when I do a isset on my second page its says on my second page "Your session is running 0". I have ensured there are no extra white places around my php session at the start of my page but i dont think thats the problem. I ahve also tried applying clear interval on my countUP function but it doesnt work? Im really thankful for any help.

page one.php

<?php session_start();
if(!isset($_SESSION["counter"])){   //checkingto see if a variable called my score exist 
$_SESSION["counter"]=0;     //if not, initates this variable with 100
  }
   ?>

page one_javascript

<script type="text/javascript">   
var counter=0; //initiates score to 100
var timer_container = document.getElementById("timer_container");  //get div that wil display the score
timer_container.innerHTML="Time =  " + counter; //popo

var timer;
 function countUP() {
 counter = counter +1;//increment the counter by 1
 //display the new value in the div
 document.getElementById("timer_container").innerHTML = counter;
 }   </script>   

page2.php

 <?php session_start();
  if(isset($_SESSION['counter'])){
  echo "Your session is running ".$_SESSION['counter'];
   }
   else { echo "Session not running";}
   ?>

page2 of javascript

<script type="text/javascript">  
var counter=<?php echo $_GET['counter'];?>; //initiates score to 100
var timer_container =document.getElementById("timer_container"); //get div that will display the score
timer_container.innerHTML="Time="+counter;

//var timer;
function countUP() {
 counter = counter+1;   //increment the counter by 1
                    //display the new value in the div
 document.getElementById("timer_container").innerHTML = counter;
 }      </script>

I have used this before in another game and it works perfect. thanks

awnstosh
  • 71
  • 5

1 Answers1

0

Haven't seen your html but you might be missing something to start and run the timer function - this might be missing:

     <body onload="intTimer = setInterval(function(){ countUP() }, 1000);">

Also I am not quite clear how you are submitting the page but I can't see anything that passes the value of the timer to the session variable. If you put a form on your page with a submit button then you could pick up the value of a hidden input with its value being set with javascript. You could submit the page automatically using submit(); in your javascript when the counter reaches a certain number if you want that:

page1.html (could be called page1,php but there is no php in page 1 so that is not actually necessary)

 <!DOCTYPE html>
 <html>
 <head>
  <title>Page 1</title>

   <script language="javascript">
   var intTimer = '';   
   var counter = 0;

   function countUP(){
   counter++; //increment the counter by 1

   //display the new value in the div
   document.getElementById("timer_container").innerHTML = "Session Time Counter: " + counter;
   document.getElementById("tim_val").value = counter;
   }
    </script>   
 </head>
       <body onload="intTimer = setInterval(function(){ countUP() }, 1000);">
              <form name="form" id="form" action="page2.php" method="post">
                  <input type="hidden" id="tim_val" name="tim_val" value="" />
                  <input type="submit" name="submit"/>
              </form>
           <div id="timer_container">Session Time Counter: 0</div>
        </body>
 </html>   

You aren't referencing $_SESSION["counter"] on page 1 and you are initialising to 0 not 100 in your JavaScript.

In your page 2 script page2.php you would have this - you could also submit this page to itself if only a part of the content is to be changed but the timer needs to continue or be logged at the point of submission.

<?php session_start();?>
<!DOCTYPE html>
<html>
<head>
<title>Page 2</title>
<?php
if(!isset($_SESSION["counter"])){
//if not, initiate this variable with 100            
$_SESSION["counter"] = 100; 
}else{
// give it the posted value
$_SESSION["counter"] = (int) $_POST['tim_val']; 
}  
?>
<script language="javascript">
clearInterval(intTimer);
var intTimer = '';  
var counter = <?php echo $_SESSION["counter"]; ?>;
function countUP(){
counter++; //increment the counter by 1
//display the new value in the div
document.getElementById("timer_container").innerHTML = "Session Time Counter: " + counter;
document.getElementById("tim_val").value = counter;
}
</script>   
</head>
    <body onload="intTimer = setInterval(function(){ countUP() }, 1000);">
          <form name="form" id="form" action="page2.php" method="post">
            <input type="hidden" id="tim_val" name="tim_val" value="<?php echo $_SESSION["counter"]; ?>" />
            <input type="submit" name="submit"/>
          </form>
        <div id="timer_container">Session Time Counter: <?php echo $_SESSION["counter"]; ?></div>
    </body>
</html>

And in your page2.php JavaScript you would need to clearInterval() and restart it. http://www.w3schools.com/jsref/met_win_clearinterval.asp

    var counter=<?php echo intval($_SESSION["counter"]);?>; //initiates score to submitted value

You probably won't need to use a session variable as you are using the hidden input, so you could use:

    var counter=<?php echo intval($_POST["counter"]);?>; //initiates score 

You could also add 1 to it to approximately account for the delay submitting it

    var counter = <?php echo $_SESSION["counter"] + 1; ?>;
    var counter=<?php echo intval($_POST["tim_val"] + 1);?>; //initiates score 

You could stick with using GET but I just prefer POST.

This has a bit more about hidden inputs: Get and echo inputs from textarea repeatedly

and a bit more about the timer script: http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock

Community
  • 1
  • 1
Steve
  • 808
  • 1
  • 9
  • 14
  • You could also submit it to itself if you were only changing content on part of the page. Glad it worked! – Steve Dec 15 '15 at 12:48