1

I want to add the existing value with the new value when the webpage refresh. I have two files which are the html and php file. The php file will show a random number between 10 and 100. while the html file perform a get jquery to get the data from the php script. Then after webpage refresh, the existing values must be incremented by the new value. For example,

the first value on the webpage is 24.
after refreshing the page, the new value is 89 from the php script
the value on the webpage must be 113 (24+89). 

I am going to show you the code below.

showtotal.php

<?php


$random = rand(10,100);
echo $random;
?>

showtotal.html

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
 updateFromServer();
    var intervalID = window.setInterval(updateFromServer, 500);

    function updateFromServer() {
        $.get({
            url: 'showtotal.php',
            dataType: 'text',
            success: totalAttacks
        });
    }

    function totalAttacks(val) {
        $('#result').html('Events Today' + val);
    }
</script>
<h1 id="result"></h1>
</body>
</html>

what my code is show different values in every refresh. How to increment the existing values by the new values? Please help me . thank you

3 Answers3

1

You can use cookies, sessionStorage or localStorage to store the lastest value. In the case of the cookie, you can get it through the PHP file, so in your Javascript file you just need to call the PHP file.

Frankusky
  • 1,040
  • 9
  • 18
0

You can put your old value in a PHP session and make this calculation in the PHP side, like this:

<?php
session_start();
if(!isset($_SESSION['old_value']))
    $_SESSION['old_value'] = 0;
$random = $_SESSION['old_value'] + rand(10,100);
$_SESSION['old_value'] = $random;
echo $random;
?>
Majid Abbasi
  • 1,531
  • 3
  • 12
  • 22
  • however, when i close the browser and open, the values start from 0. If i want to use permanent storage –  Nov 02 '17 at 02:46
  • 1
    in this case you are force to save value in the database, session will remove when you close browser. simple save and get $_SESSION['old_value'] from database – Majid Abbasi Nov 02 '17 at 05:35
  • if i do not want to use database, any other storage that has the ability to be permanent –  Nov 02 '17 at 06:04
  • 1
    As this data is not an important can save latest status in a file and every-time read and write on that instead of using session – Majid Abbasi Nov 02 '17 at 06:12
0

You can store previous value in a hidden field.

function totalAttacks(val) {
    var pre_val = parseInt($('#prev').val());
    var new_val = pre_val+parseInt(val);
    $('#result').html('Events Today' + new_val);
    $('#prev').val(new_val);
}

Also add this to yr html

<input type="hidden" id="prev" value="0">
chippy
  • 74
  • 10
  • I believe this wont work, after refreshing the browser will load your DOM value instead of retrieving the last value – Frankusky Oct 31 '17 at 14:22