PHP session variable not parsing but ISSET says it is running & at 0

前端 未结 1 569
南笙
南笙 2021-01-23 16:00

Ok, so I have a counter which simply counts up a second at a time.This second counter I want it to be my session variable.Is this a bad idea? Also when I do a isset on my second

相关标签:
1条回答
  • 2021-01-23 16:25

    Haven't seen your html but you might be missing something to start and run the timer function - this might be missing:

         <body onload="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>
    
       <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>   
    

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

    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=<?php echo 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=<?php echo intval($_POST["counter"]);?>; //initiates score 
    

    You could also add 1 to it to approximately account for the delay submitting it

        var counter = <?php echo $_SESSION["counter"] + 1; ?>;
        var counter=<?php echo 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

    0 讨论(0)
提交回复
热议问题