phpactionscript-3

Passing variable from AS3 to PHP


I am currently doing a flash game that records the time taken and sending it to a 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. 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. Thanks in advance!

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:

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

Solution

  • 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. The main issue was the absolute Url issue and i did not do anything to handle the result properly in php. Thanks everyone for throwing in the issues. I appreciate!