2017-10-03 9 views
-1

私はjavacriptコードを持っています。私はこれらのコードをオンラインで購入しています。コードはスピンホイールゲームを実行します。私はスピンドルの結果をデータベースに保存したいのですが、 実際にjsコードはすでに結果関数を提供していますが、結果関数からmysqlデータベースに保存する方法を知りませんでした。JavaScriptのコンソールを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();

誰かがチュートリアルや私の問題のいくつかの参照を与えるために私を助けることができますか?よろしくお願いいたします。

+0

文字列に変換jsのコード 'JSON.stringify(jscode)を使用して、'、データベースに保存し、データベースからのことをフェッチユーザー ':https://code.jquery.com/
にconsole.logステートメントを使用した後:リンク - CDNにより、 JSON.parse(データベースから) '、実際のjsコード用 –

答えて

0

Ajaxはまだあなたのために働くいない場合、私は、サーバー側でご使用してPHPを想定していますが、あなたは、サーバースクリプトをカスタマイズする必要があります。

あなたは
追加jqueryのスクリプトを.PHPするためにJavaScriptからのデータを共有するためのAjaxを必要としますその後、

$.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'];  
} 
関連する問題