如何将JQuery变量发布到数据库

时间:2017-05-12 17:13:50

标签: javascript php jquery html postgresql

我尝试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。现在,它将分数发送到数据库。

1 个答案:

答案 0 :(得分:1)

您需要一些服务器端代码来处理传入的数据并将其粘贴到您的数据库中。 PHP,Django / Python和Ruby on Rails都是不错的选择。然后你可以在javascript中编写一个AJAX函数,在游戏结束时将分数数据发送到服务器。