【发布时间】:2017-10-12 02:17:57
【问题描述】:
我尝试了 nikola 的 javascript 打字游戏。这是链接codepen.io/nikola1970/pen/oxXbmb,当时间到时,我想将我得到的分数发布到我的数据库上的分数表中。
CREATE TABLE public.score
(
id bigserial,
score integer,
user character varying(50),
"timestamp" timestamp without time zone,
)
我该怎么办?
我这样添加倒计时功能。
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);
}
这是我的 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()
?>
我已经编辑了我的倒计时函数和 ajax.php。现在可以将分数发送到数据库了。
【问题讨论】:
-
您可以使用 AJAX 调用 API,该 API 可以将数据保存回您的数据库。
-
您应该尝试自己编写代码。在doing more research 之后,如果您有问题发布您尝试过的方法,并清楚地解释什么不起作用,并提供a Minimal, Complete, and Verifiable example。阅读How to Ask 一个好问题。请务必take the tour 并阅读this。
标签: javascript php jquery html postgresql