【问题标题】:passing json object data to a php file将 json 对象数据传递给 php 文件
【发布时间】:2020-03-21 05:08:42
【问题描述】:

我的编码经验很少,(Bigtimenoob)但最近购买了一些我需要编辑的代码。

我希望将结果发送到 PHP 文件,以便进行记录。我想将结果发送到wheelresults.php,我设置如下:

<html>
<body>
   <?php
       $file = fopen("wheeldata.txt","a");
       fclose($file);
   ?>
</body>
</html>

但是如何从对象中获取信息到这个文件中呢?

// 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.json', 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);
    //reset the wheel at the end of the game after 5 seconds
    /*  TweenMax.delayedCall(1, function(){
    Spin2WinWheel.reset();
    })*/
}

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

【问题讨论】:

    标签: php json object


    【解决方案1】:

    啊,是的,Chris Gannon 的经典作品Spin2WinWheel

    myGameEnd 函数中,您想将e 发送到您的wheelresults.php

    这可以通过 AJAX 调用来完成:

    function myGameEnd(e)
    {
        //e is gameResultsArray
        console.log(e);
        var request = new XMLHttpRequest();
        request.open('POST', '/wheelresults.php', true);
        request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
        request.send("result=" + encodeURIComponent(JSON.stringify(e)));
    }
    

    请注意,您当前的 PHP 代码实际上并没有向wheeldata.txt 文件写入任何内容。

    您可能想为此使用file_put_contents

       <?php
            file_put_contents(
                "wheeldata.txt",
                filter_var($_POST['result'], FILTER_VALIDATE_STRING),
                FILE_APPEND
            );
        ?>
    

    【讨论】:

      猜你喜欢
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 2020-07-12
      • 1970-01-01
      • 2014-06-12
      • 1970-01-01
      相关资源
      最近更新 更多