;

PDA

Click to See Complete Forum and Search --> : URLLoader issue - AS3 to PHP and back again


geniusprinting
07-02-2009, 08:32 AM
Hi,

I'm just testing out sending variables between AS3 and PHP.

The code below sends variables to PHP fine.

PHP returns a string "returnedfirstname=bob" just fine. I've tested that by using: trace(loader.data).

The problem is, I can't seem to create a variable in AS3 called returnedfirstname and populate it with "bob" from the loader.data:

var request:URLRequest = new URLRequest ("<someurl>");
request.method = URLRequestMethod.POST;

var variables:URLVariables = new URLVariables();

variables.firstName = "Bob";
request.data = variables;

var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);

function onComplete (event:Event):void {
var returnedfirstname = loader.data.returnedfirstname;
trace (returnedfirstname);
};

trace(returnedfirstname) gives me nothing.

florianvanthuyn
07-02-2009, 06:27 PM
Try out this and see if it works:

function onComplete (event:Event):void {
var returnedfirstname:String = event.target.data.returnedfirstname;
trace (returnedfirstname);
};

geniusprinting
07-02-2009, 09:25 PM
Thanks florianvanthuyn but I get the same results.

to confirm, I put in:

switch(loader.dataFormat) {
case URLLoaderDataFormat.TEXT :
trace("completeHandler (text): " + loader.data);
break;
case URLLoaderDataFormat.BINARY :
trace("completeHandler (binary): " + loader.data);
break;
case URLLoaderDataFormat.VARIABLES :
trace("completeHandler (variables): " + loader.data);
break;
}

and get the result:

completeHandler (variables): returnedfirstname=bob

So it's definitely getting, as variables, the right data back into Flash but:

returnedfirstname = event.target.data. returnedfirstname

return null

as does

returnedfirstname = variables. returnedfirstname

florianvanthuyn
07-03-2009, 02:33 AM
I'll just post how I managed to get it work one time with the code I gave, so maybe it's the way you send it back with your php?

<?php
// receive variables
$input_name = $_POST['user'];
$input_pw = $_POST['password'];

// checking if valid
if($input_name == "admin" && $input_pw == "password")
{
print "login=accepted";
}
else
{
print "login=denied";
}
?>

So far the php, now tha as3:

var myURL:String = "scrapbook/config/login.php";

var myVars:URLVariables = new URLVariables();
myVars.user = login.user.text;
myVars.password = login.pw.text;
myVars.sendRequest = "parse";

var myUrlReq:URLRequest = new URLRequest(myURL);
myUrlReq.data = myVars;
myUrlReq.method = URLRequestMethod.POST;

var myUrlLoader:URLLoader = new URLLoader();
myUrlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myUrlLoader.addEventListener(Event.COMPLETE, successHandler);

myUrlLoader.load(myUrlReq);

private function successHandler(e:Event):void
{
if(e.target.data.login == "accepted")
{
// correct stuff
}
else
{
// incorrect stuff
}
}