简体   繁体   English

将变量从 AS3 传递给 PHP

[英]Passing variable from AS3 to PHP

I am currently doing a flash game that records the time taken and sending it to a php.我目前正在做一个 Flash 游戏,记录所用的时间并将其发送到 php。 Currently, my game is working and the time taken could be traced correctly.目前,我的游戏正在运行,并且可以正确跟踪所花费的时间。 URLLoader is also working correctly however, my php page does not seem to be able to get the result from $_POST. URLLoader 也工作正常,但是,我的 php 页面似乎无法从 $_POST 获得结果。 I have posted my code below.我已经在下面发布了我的代码。

I would appreciate any help given.我将不胜感激任何帮助。 Have already tried various of methods but just could not work.已经尝试了各种方法,但都无法奏效。 I either get the processScore.php opening on a separate window and showing empty page or nothing happen at all.我要么在单独的窗口上打开 processScore.php 并显示空页面,要么什么也没有发生。 Thanks in advanced!提前致谢!

AS3: AS3:

function sendTime() {
        var req:URLRequest = new URLRequest("processScore.php");
        var loader:URLLoader = new URLLoader();
        //using URLLoaderDataFormat.VARIABLES give me error so I changed to TEXT.
        loader.dataFormat = URLLoaderDataFormat.TEXT;
        var toSend:URLVariables = new URLVariables();
        req.data = toSend;
        req.method = URLRequestMethod.POST;

        toSend.timeRecorded = timeTaken;

        loader.addEventListener(Event.COMPLETE, timeSent);
        loader.load(req);

    }

    function timeSent(e:Event) {
        trace("SENT");
    }

processScore.php: processScore.php:

<?php 
  if(isset($_POST['timeRecorded'])) {
    echo $_POST['timeRecorded'];
}
?> 

Try this尝试这个

function sendTime() {

        var loader:URLLoader = new URLLoader();
        var req:URLRequest = new URLRequest("processScore.php");
        var toSend:URLVariables = new URLVariables();
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        req.method = URLRequestMethod.POST;
        toSend.timeRecorded = timeTaken;
        req.data = toSend;
        loader.addEventListener(Event.COMPLETE, timeSent);
        loader.load(req);

    }

    function timeSent(e:Event) {
        trace("SENT");
    }

PHP: PHP:

<?php 
        $timeRecorded = $_POST['timeRecorded'];
         echo $timeRecorded;
    ?>

Managed to solve the problem.设法解决了问题。 I did not really figure out the main idea behind URLLoader and hence I thought there was a problem when the flash page remains.我没有真正弄清楚 URLLoader 背后的主要思想,因此我认为 Flash 页面保留时存在问题。 The main issue was the absolute Url issue and i did not do anything to handle the result properly in php.主要问题是绝对的 Url 问题,我没有做任何事情来正确处理 php 中的结果。 Thanks everyone for throwing in the issues.感谢大家提出问题。 I appreciate!我很欣赏!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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