Yo, how to store variable? [javascript]

Andyoyo

TH.net Regular
Reaction score
22
I just started learning javascript today, the main question I have is how to keep a variable stored in your website for later use, as in it does not reset. I thought a good learning example would be to try to make a reputation variable, that stores this variable somehow in the ftp? heres my code so far:
Code:
<html>
<head>
<script type="text/javascript">

function disp_alert()
{
var username
username = "Andyoyo"

rep = rep + 1
alert(username + "'s Reputation Increased!" + '\n' + username + "'s total Reputation: " + rep);
}
</script>
</head>
<body>

<input type="button" onclick="disp_alert()" value="Add to this user's reputation" />

</body>
</html>



<html>
<head>
<script type="text/javascript">
function disp_confirm()
{
var r=confirm("New User?");
if (r==true)
  {
  document.write("Reset Reputation");

  }
else
  {
  document.write("Reputation value contained.");
  }
}
</script>
</head>
<body>

<input type="button" onclick="disp_confirm()" value="Display a confirm box" />

</body>
</html>






//UserReputation.js
if (rep == 0)
function disp_alert()
{
var username
username = "Andyoyo"
var rep
rep = 0
alert(username + "'s Reputation Increased!" + '\n' + username + "'s total Reputation: " + rep);
}
else
</script>
</head>
<body>

<input type="button" onclick="disp_alert()" value="Add to this user's reputation" />


I'm trying to find a way to store that 'rep' variable without it reseting when a user visits. I also am trying to have it so a user can only vote once per visit, but that can be handled later.
 

SFilip

Gone but not forgotten
Reaction score
633
JavaScript runs on client-side meaning that you can't store anything on the server, at least not directly (keep reading).
That said, you can use cookies, but be aware that the user can delete/modify these at any point. See this article for more info.
For storing something like reputation, you need to redirect to some PHP/ASP/whatever script (or possibly use XMLHttpRequest, google for more info on that).
 

GooS

Azrael
Reaction score
154
To make exactly this, you would have to use AJAX (no reload in page and information can be presented using javascript) and this AJAX will relay to a PHP script that pulls the information out of a database or where ever you store the information(variable)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top