【问题标题】:how to save javascript console to the Mysql Database如何将javascript控制台保存到Mysql数据库
【发布时间】:2018-03-14 05:07:21
【问题描述】:

我有一个 javacript 代码,我在网上购买这些代码。代码运行旋转轮游戏。我想将旋转轮的结果保存到数据库中, 实际上js代码已经提供了result函数,但是我不知道如何将result函数保存到mysql数据库中。

代码如下:

//Usage

//load your JSON (you could jQuery if you prefer)
function loadJSON(callback) {

  var xobj = new XMLHttpRequest();
  xobj.overrideMimeType("application/json");
  xobj.open('GET', './wheel_data.php', true); 
  xobj.onreadystatechange = function() {
    if (xobj.readyState == 4 && xobj.status == "200") {
      //Call the anonymous function (callback) passing in the response
      callback(xobj.responseText);
    }
  };
  xobj.send(null);
}

//your own function to capture the spin results
function myResult(e) {
  //e is the result object
    console.log('Spin Count: ' + e.spinCount + ' - ' + 'Win: ' + e.win + ' - ' + 'Message: ' +  e.msg);

    // if you have defined a userData object...
    if(e.userData){
      
      console.log('User defined score: ' + e.userData.score)

    }

  //if(e.spinCount == 3){
    //show the game progress when the spinCount is 3
    //console.log(e.target.getGameProgress());
    //restart it if you like
    //e.target.restart();
  //}  

}

//your own function to capture any errors
function myError(e) {
  //e is error object
  console.log('Spin Count: ' + e.spinCount + ' - ' + 'Message: ' +  e.msg);

}

function myGameEnd(e) {
  //e is gameResultsArray
  console.log(e);
  TweenMax.delayedCall(5, function(){
    /*location.reload();*/
  })


}

function init() {
  loadJSON(function(response) {
    // Parse JSON string to an object
    var jsonData = JSON.parse(response);
    //if you want to spin it using your own button, then create a reference and pass it in as spinTrigger
    var mySpinBtn = document.querySelector('.spinBtn');
    //create a new instance of Spin2Win Wheel and pass in the vars object
    var myWheel = new Spin2WinWheel();
    
    //WITH your own button
    myWheel.init({data:jsonData, onResult:myResult, onGameEnd:myGameEnd, onError:myError, spinTrigger:mySpinBtn});
    
    //WITHOUT your own button
    //myWheel.init({data:jsonData, onResult:myResult, onGameEnd:myGameEnd, onError:myError);
  });
}



//And finally call it
init();

有人可以帮我为我的问题提供教程或参考吗? 谢谢。

【问题讨论】:

  • 使用 JSON.stringify(jscode) 将 js 代码转换为字符串并保存到数据库中,当你从数据库中获取它时,用户 JSON.parse(from database) ,用于实际的 js 代码

标签: javascript php mysql ajax post


【解决方案1】:

我假设您在服务器端使用 PHP,如果不是 Ajax 仍然可以为您工作,但您必须自定义服务器脚本:

您需要 Ajax 将数据从 javascript 共享到 .php
通过 cdn 添加 jquery 脚本 - 链接:https://code.jquery.com/
在 console.log 语句后使用:

$.ajax({
        type: 'post',
        url: "sample.php", // replace your with .php file which will receive data 
        data: { 'spinCount':  e.spinCount, 'Win': e.win },

        success: function(result){

            console.log( result ); // Do what you want if data successfully trasfered.
        }
});




if( isset( $_POST['Win'] && $_POST['spinCount'] ) ){
    // store these in database
    $_POST['Win'];  
    $_POST['spinCount'];    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 2014-01-19
    相关资源
    最近更新 更多