0

I have a working countup timer in tutorial1.php file. What I want to do is when user click Stop button the timer will stop and send the duration to result.php file. The timer is in javascript, but I am new in javascript. Right now I am trying using session but it doesn't work. I don't want to use post or get because I use it for another calculation.

Below is the code:

1) tutorial1.php

<?php session_start(); ?>
<h2 class="pull-right label label-primary"> Timer : <span id='timer'></span> </h2> //the countup timer
<?php $_SESSION['time'] = 'myVar'; ?>
<button id='".$j."' class='next btn btn-primary finish' onclick='myStopFunction()' name='Finish' type='submit'>Stop it!</button> //Stop button

<script>
    //TIMER
    var myVar = setInterval(function(){ timedCount() }, 1000);
    var c = 0;
    var t;
    timedCount();

    function timedCount() {

        var hours = parseInt( c / 3600 ) % 24;
        var minutes = parseInt( c / 60 ) % 60;
        var seconds = c % 60;

        var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);


        $('#timer').html(result);
        c = c + 1;
    }

    function myStopFunction() {
        clearInterval(myVar);

    }
</script>

2) result.php

Your time is <?php echo $_SESSION['time']; ?>

How to make it work? Or is there any other easy solution for my problem? Thanks in advance.

  • 2
    Cookies and `localStorage` are possible ways to pass values between pages and on the same page too. With `localStorage`, you would load the value with Javascript during the `window.onload` and not even worry about PHP. With cookies, there is no issues with PHP or Javascript. – Tigger Sep 25 '17 at 08:16
  • If you want backward compatibility then you can use AJAX with PHP `$_SESSION`s. – StackSlave Sep 25 '17 at 08:20
  • I still don't get it. Sorry I am new in javascript :( – I-man Jasmienz Sep 25 '17 at 08:41
  • check this https://stackoverflow.com/questions/3590293/set-session-variable-using-javascript-in-php – スージン Sep 25 '17 at 09:05
  • @Tigger can you write the working code please. I tried but not working – I-man Jasmienz Sep 26 '17 at 02:27

2 Answers2

0

You can use PHP cookies.

<?php 
setcookie("name","value",time()+$int);
/*name is your cookie's name
value is cookie's value
$int is time of cookie expires*/
?>

Getting Cookie
=============================
<?php 
echo $_COOKIE["your cookie name"];
?>
rafon
  • 1,469
  • 11
  • 21
0

Try add cookie in js script: https://www.w3schools.com/js/js_cookies.asp set and get from another page (but js maybe disallow get cookies from different domains)

But in my opinion better solution: use jQuery $.get (https://api.jquery.com/jquery.get/) to send request to php file with (you can use multiple requests in one page):

// timeupdate.php
<?php
// timestamp update from another page
// https://url/timeupdate.php?time=123456 
$time = (int)$_GET['time'];
$_SESSION['time'] = $time;
?>
?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459