Sending and receiving data from Flash AS3 to PHP

Solution 1:

Your AS code seems to be right. So the problem might be in PHP. Please test first with this PHP file:

<?php
       echo "test=1&done=true";    
?>

This should then let your movie trace "true". You then should debug your PHP. print_r($_POST); destroys your output of course. May be you did forget to remove this debugging statement :-)

@Jesse and @Ascension Systems, check the docs for URLVariables: http://livedocs.adobe.com/flash/9.0_de/ActionScriptLangRefV3/flash/net/URLVariables.html

Solution 2:

Try

submitbtn.addEventListener(MouseEvent.CLICK, sendData);

function sendData(event:MouseEvent):void
{
  var urlreq:URLRequest = new URLRequest ("http://[mydomain]/test.php");
  urlreq.method = URLRequestMethod.POST; 

  var urlvars:URLVariables = new URLVariables(); 
  urlvars.uname = nametxt.text;
  urlvars.apellido = aptxt.text;
  urlvars.email = emtxt.text;
  urlvars.cedula = cctxt.text;
  urlvars.score = scoretxt.text;
  urlreq.data = urlvars;          

  var loader:URLLoader = new URLLoader (urlreq); 
  loader.addEventListener(Event.COMPLETE, completed); 
  loader.dataFormat = URLLoaderDataFormat.VARIABLES; 
  loader.load(urlreq); 
}

public function completed (event:Event):void{
  var variables:URLVariables = new URLVariables( event.target.data );
  resptxt.text = variables.done;
}

Updated the completed function... and corrected missing bracket.

Solution 3:

First of all, change this line of code:

trace(loader2.data.done);

to this:

trace(loader2.data);

You're outputting raw text from php, so your data object in flash is just gonna be raw text. It's not an object with .done attached to it. If you want to have a data structure then you need to create some XML or something inside PHP, print that out and then cast loader2.data as XML, like so:

var returnedData:XML = new XML(loader2.data);

However, if your XML is not formed correctly, you'll create an uncaught error in flash and crash your app, so make sure you use try/catch statements.