【问题标题】:PHP session variable not parsing but ISSET says it is running & at 0PHP会话变量未解析但ISSET说它正在运行&在0
【发布时间】:2015-12-12 15:30:26
【问题描述】:

好的,所以我有一个计数器,它一次只计数一秒。我希望第二个计数器成为我的会话变量。这是个坏主意吗?此外,当我在第二页上执行 isset 时,它会在我的第二页上显示“您的会话正在运行 0”。我已经确保在我的页面开始时我的 php 会话周围没有多余的白色区域,但我认为这不是问题所在。我也尝试在我的 countUP 函数上应用明确的间隔,但它不起作用?我真的很感谢你的帮助。

第一页.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
  }
   ?>

第一页_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";}
   ?>

javascript的第2页

<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>

我之前在另一个游戏中使用过它,它运行良好。谢谢

【问题讨论】:

    标签: javascript php


    【解决方案1】:

    尚未看到您的 html,但您可能缺少启动和运行计时器功能的内容 - 这可能缺少:

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

    我也不太清楚你是如何提交页面的,但我看不到任何将计时器的值传递给会话变量的东西。如果您在页面上放置一个带有提交按钮的表单,那么您可以获取隐藏输入的值,其值是使用 javascript 设置的。如果您需要,当计数器达到某个数字时,您可以在 javascript 中使用 submit(); 自动提交页面:

    page1.html(可以称为page1,php,但第1页中没有php,因此实际上没有必要)

     <!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>   
    

    您没有在第 1 页引用 $_SESSION["counter"],并且您在 JavaScript 中初始化为 0 而不是 100。

    在您的第 2 页脚本 page2.php 中,您将拥有这个 - 如果仅要更改部分内容但计时器需要继续或在提交时记录,您也可以将此页面提交给它自己。

    <?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>
    

    在您的 page2.php JavaScript 中,您需要 clearInterval() 并重新启动它。 http://www.w3schools.com/jsref/met_win_clearinterval.asp

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

    您可能不需要使用会话变量,因为您正在使用隐藏输入,因此您可以使用:

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

    您也可以在其中添加1 以大致说明提交延迟的原因

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

    你可以坚持使用GET,但我更喜欢POST

    这有更多关于隐藏输入的内容: Get and echo inputs from textarea repeatedly

    还有更多关于计时器脚本的信息: http://www.w3schools.com/js/tryit.asp?filename=tryjs_timing_clock

    【讨论】:

    • 如果您只是更改部分页面的内容,也可以将其提交给自己。很高兴它成功了!
    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 2021-01-07
    相关资源
    最近更新 更多