简体   繁体   English

如何通过php从另一个页面获取javascript变量值

[英]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 $.gethttps://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.

相关问题 如何从另一个页面获取URL值JavaScript / PHP - How to get a URL value from another page JavaScript / PHP 从PHP页面获取值到另一个页面中的Javascript - Get a value from a PHP Page to a Javascript in Another Page Issue 如何在不获取或发布的情况下将JavaScript变量值从一页获取到另一页 - How to get javascript variable value from one page to another without get or post PHP / JavaScript:如何将变量从一页传递到另一页 - PHP / JavaScript: How to pass variable from one page to another 如何从依赖下拉列表获取价值到javascript并将其解析到另一个php页面? - how to get value from dependant drop down list to javascript and parsing it to another php page? 我们如何从 javascript 变量中获取 php 变量中的值 - How can we get value in php variable from javascript variable 从另一个页面更新JavaScript变量的值 - Update a value of a JavaScript variable from another page 如何在第二页上将值从Javascript传递给php变量? - How to pass value from Javascript to php variable on the second page? 将 javascript 字符串变量传递到另一个页面并使用 AJAX 在 PHP 中获取变量 - Pass javascript string variable to another page and get the variable in PHP with AJAX 如何从另一个页面的JavaScript获取文本字段值 - How to get text field value from another page javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM