0

<?php
      $myFile = "new.txt";
    $fileOpen = fopen($myFile, 'w') or die("can't open file");
    $stringData = "total;
    fwrite($fileOpen, $stringData);
    fclose($fileOpen);
      ?>
<html>
<head>
<script>
         var defaultValue = 5;
         var plus = 1;
         var max = 100;
         var total = defaultValue;
         
     window.setInterval(

     function () {

        
         if (total > max){
         
         total = defaultValue;

         }
        
         document.getElementById("demo1").innerHTML = total;


                        total = total + plus;

     }, 1000);
         
</script>
</head>
<body>

<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>

</body>
</html>

I need save the total value into txt file, and than read by txt. so that, when i refresh the browser will not re-looping, but how i get value from javascript or from div into php? (read and write per second)

Ricky
  • 50
  • 9

2 Answers2

1

You can use an AJAX request to a php file where you can save it to txt file or a database.

But, making an ajax call every second would be unnecessary traffic. Instead I think using window.onunload would be a better choice, which saves the value when the page is redirected or refrshed.

You can use the jquery bind for that as below:

$(window).bind('beforeunload', function(){
  // make ajax call here
});
anirudh
  • 4,116
  • 2
  • 20
  • 35
-1

If you want to save very small amount of data, like here you need to save the value of total then you can use HTML 5 provided local storage which can easily contain on average 5 MB of data. Its nothing but a storage in each domain(origin) which can store 5 MB on any individual client. It is very easy to use like.

  enter code here

     // Store
    localStorage.totalValueHolder = total;
    // Retrieve
   var totalStringValue = localStorage.totalValueHolder;
    total = parseInt(totalStringValue, 10)
OR
// Store`enter code here`
localStorage.setItem("totalValueHolder ", total);
// Retrieve
var totalStringValue = localStorage.getItem("totalValueHolder");
total = parseInt(totalStringValue, 10)

Hope it will answer your question. If you have further query please let us know.

Abdullah
  • 935
  • 9
  • 18
  • This is can work, but i want save in the storage permanently, use any browser any computer can do the calculation continuously... (like a 'Jackpot') can you give me some example... – Ricky Aug 25 '15 at 09:25