Javascript is interpreted and executed by the browser and with each new execution of Browse the javascript is reloaded with its default values.
How to save the data and even with refresh get the values again?
Session is a feature that allows you to save values (variables) to be used throughout the user’s visit. Saved session values can be used in any part of the script, even on other pages of the site. Variables that remain set until the visitor closes the browser or the session is destroyed.
Javascript:
...
<script>
localStorage.setItem('chave','valor');
alert(localStorage.getItem('chave'));
</script>
...
PHP:
You need to log in before you can set or pick up values from it. There is no limit to values saved in the session. The session is personal to each visitor. When a visitor visits the site, a cookie is generated on his computer informing him of a unique session id and PHP uses this identifier to organize the sessions between visitors to his site. But this cookie is valid only as long as the browser is open.
- To open the session just use this command in PHP:
..
<?php
session_start(); // Inicia a sessão
- After logging in you can set values within it in this way:
..
<?php
$_SESSION['preco'] = 100;
- And when you need to display the value saved in the session (probably on other pages), just do so:
..
<?php
echo $_SESSION['preco']; // Resultado: 100
- You can save as many values as you want, you can re-define the values and use them in Snippets, function arguments and the way you prefer.
To delete a session-specific variable you use unset():
..
<?php
unset($_SESSION['preco']); // Deleta uma variável da sessão
- You can also destroy the entire session at once by deleting all the variables saved in it:-
..
<?php
session_destroy(); // Destrói toda sessão
Documentación Oficial:
Function session_start() » Start the session
Function unset() » Delete a PHP variable
Function session_destroy() » Destroys a whole session and its variables
Have you ever thought about using Cookies? generally this data is saved in server side session.
– user3010128