[英]How to get javascript variable value from another page by php
I have a working countup timer
in tutorial1.php
file. 我在
tutorial1.php
文件中有一个工作的递增timer
。 What I want to do is when user click Stop
button the timer will stop and send the duration to result.php
file. 我想做的是,当用户单击“
Stop
按钮时,计时器将停止并将持续时间发送到result.php
文件。 The timer is in javascript, but I am new in javascript. 计时器在javascript中,但是我是javascript新手。 Right now I am trying using
session
but it doesn't work. 现在,我正在尝试使用
session
但是它不起作用。 I don't want to use post or get because I use it for another calculation. 我不想使用post或get,因为我将其用于其他计算。
Below is the code: 下面是代码:
1) tutorial1.php 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 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.
提前致谢。
You can use PHP cookies . 您可以使用PHP cookie 。
<?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"];
?>
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) 尝试在js脚本中添加cookie: https : //www.w3schools.com/js/js_cookies.asp设置并从另一个页面获取(但是js可能不允许从不同域获取cookie)
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): 但是我认为更好的解决方案是:使用jQuery
$.get
( https://api.jquery.com/jquery.get/ )将请求发送到php文件(您可以在一个页面中使用多个请求):
// timeupdate.php
<?php
// timestamp update from another page
// https://url/timeupdate.php?time=123456
$time = (int)$_GET['time'];
$_SESSION['time'] = $time;
?>
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.