简体   繁体   中英

how to save javascript console to the Mysql Database

i have a javacript code, i buy these code online. the code run the spinwheel game. and i want to save the result from spinwheel into database, actually the js code already provide the result function but i didnt know how to save from the result function into mysql database.

here's the code:

 //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();

can someone help me to give a tutorial or some reference for my problems? thankyou.

I am assuming your using PHP at server end, if not Ajax will still work for you but you have to customize server script:

You need Ajax to share data from javascript to .php
add jquery script by cdn - link: https://code.jquery.com/
After console.log statement use :

$.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'];    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM