Poh Zi How
Poh Zi How

Reputation: 1569

Saving javascript vars onto server

I am quite new to javascript and am currently finding a way to manage a high score system for a simple online game. The original plan was just to store the updated vars onto a server and retrieving them upon request eg

var score = 0;
var level = 0;
//javascript game code to update score and level
//code to store vars into database

Upon some research however it seems that it is not recommended for javascript to directly communicate with the server-side and should instead communicate via some sort of server-side scripting language (eg PHP), which can probably be done so using a form.

Now I'm pretty confused as to what I should do, as all I want to do is to store some variables which are already created without having to create a form. I have also thought about saving the variables to a text file, but that does not seem to be a solution ideal for an online score system.

To top it all off many other keywords started springing up (eg AJAX, jQuery, nodeJS, JSON, parse... to name a few) which I'm not too sure if they're relevant to my current problem.

What is the simplest way to go about doing this (storing variables online to be retrieved upon request)?

Upvotes: 0

Views: 137

Answers (3)

Arnial
Arnial

Reputation: 1441

I would not recommend it, because of poor security, but it is possible if database has HTTP API (many noSQL and some SQL databases has it). With http api support you can send Ajax requests, directly to database, to save and receive data.

For example: CouchDB, OrientDB

Upvotes: 1

durbnpoisn
durbnpoisn

Reputation: 4669

It's not only recommended to use server side code. It's the only way.

Start with getting your server-side set up. Then, write your javascript to make an AJAX call to it.

The full explanation is like this... Your PHP (or ASP) page can be made to talk to the database, and do whatever - read data, write data, update records. When it's finished, it gives a result. This happens every time you communicate with the page.

The server couldn't care less what you do with the output. It just makes it. AJAX is a method of using JavaScript to contact that PHP page, and wait for the result. In this manner, you can contact that page, get your result, and not have to refresh the current page.

This is a pretty widely used method.

To give you an idea what this looks like in a very simple fashion:

function countClick(id,host) {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
         //document.getElementById("demo").innerHTML = xhttp.responseText;
        }
      };
      xhttp.open("GET", "/includes/appCounter.php?appid="+ id +"&hostIs="+ host, true);
      xhttp.send();
    }

What this does is, when the function is triggered, it calls a PHP script and passes in the values for 2 variables (an appID and whichButton). The PHP script simply takes those values and makes use of them (increments a counter in the database).

That's it. The database is updated with the new values, and the user never even knows it happened.

Upvotes: 1

Williz
Williz

Reputation: 309

Yes you will need to setup a server for this using PHP or similar. If you want to use server side javascript, you can look into setting up a node.js server

Upvotes: 1

Related Questions