-2

I try javascript typing game by nikola. Here is the link codepen.io/nikola1970/pen/oxXbmb and When the time's up i want to post the points i get to the score table on my database.

CREATE TABLE public.score
(
  id bigserial,
  score integer,
  user character varying(50),
  "timestamp" timestamp without time zone,
)

how can i do?

I add the countdown function like this.

function countdown() {
    points = 0;
    var timer = setInterval(function(){
        button.disabled = true;
        seconds--;
        temp.innerHTML = seconds;
        if (seconds === 0) {

                if (window.XMLHttpRequest) {
                    xmlhttp = new XMLHttpRequest();
                }

                xmlhttp.open("GET", "ajax.php?points=" + points , true);
                xmlhttp.send();

               // return false;

            alert("Game over! Your score is " + points);
             //Done!
            scoreDiv.innerHTML = "0";
            words.innerHTML = "";
            button.disabled = false;
            clearInterval(timer);
            seconds = 60;
            timerDiv.innerHTML = "60";
            button.disabled = false;    
        }
    }, 10);
}

and here is my ajax.php

<?php
session_start();

 include("config.php");


$score = $_GET['points'];
$user = $_SESSION['id'];


$query = pg_query(" insert into t_score (score, id_user, timestamp) values ('$score', $user, 'now()') ") ;
pg_close()
?>

i've editted my countdown function and ajax.php. now it's work to send the score to the database.

adn
  • 430
  • 2
  • 7
  • 20
  • You can use AJAX to make a call to an API, and that API can save the data back to your DB. – Nisarg Shah May 12 '17 at 17:22
  • You are expected to try to **write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard May 12 '17 at 17:50

1 Answers1

1

You're going to need some server-side code to process incoming data and stick it in your database. PHP, Django/Python, and Ruby on Rails are all good choices. You could then write an AJAX function in the javascript that sends the score data to the server when the game is over.

MikeC
  • 480
  • 6
  • 14