【问题标题】:How to post JQuery variable into database [duplicate]如何将JQuery变量发布到数据库中[重复]
【发布时间】: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。现在可以将分数发送到数据库了。

【问题讨论】:

标签: javascript php jquery html postgresql


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-03-27
    • 2021-10-22
    • 2019-04-28
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 2013-11-09
    • 2023-03-17
    相关资源
    最近更新 更多