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.