Php Session Variable Not Parsing But Isset Says It Is Running & At 0
Solution 1:
Haven't seen your html but you might be missing something to start and run the timer function - this might be missing:
<bodyonload="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><scriptlanguage="javascript">var intTimer = '';
var counter = 0;
functioncountUP(){
counter++; //increment the counter by 1//display the new value in the divdocument.getElementById("timer_container").innerHTML = "Session Time Counter: " + counter;
document.getElementById("tim_val").value = counter;
}
</script></head><bodyonload="intTimer = setInterval(function(){ countUP() }, 1000);"><formname="form"id="form"action="page2.php"method="post"><inputtype="hidden"id="tim_val"name="tim_val"value="" /><inputtype="submit"name="submit"/></form><divid="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><?phpif(!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'];
}
?><scriptlanguage="javascript">clearInterval(intTimer);
var intTimer = '';
var counter = <?phpecho$_SESSION["counter"]; ?>;
functioncountUP(){
counter++; //increment the counter by 1//display the new value in the divdocument.getElementById("timer_container").innerHTML = "Session Time Counter: " + counter;
document.getElementById("tim_val").value = counter;
}
</script></head><bodyonload="intTimer = setInterval(function(){ countUP() }, 1000);"><formname="form"id="form"action="page2.php"method="post"><inputtype="hidden"id="tim_val"name="tim_val"value="<?phpecho$_SESSION["counter"]; ?>" /><inputtype="submit"name="submit"/></form><divid="timer_container">Session Time Counter: <?phpecho$_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=<?phpecho 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=<?phpecho intval($_POST["counter"]);?>; //initiates score
You could also add 1
to it to approximately account for the delay submitting it
var counter = <?phpecho$_SESSION["counter"] + 1; ?>;
var counter=<?phpecho 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
Post a Comment for "Php Session Variable Not Parsing But Isset Says It Is Running & At 0"