PDA

View Full Version : Variables with URLLoader



RvGaTe
July 21st, 2009, 10:34 AM
Heyaaa people,

It has been to long that i've been here, last post dates from 2005 :cantlook:

Recently i got job that finaly includes some AS3, so im getting back into the world of flash again. But as you all can imagen, not without problems... let me explain what i'm trying to get working:

A little background:
We are currently recreating a game within flash, the game requires a login to create a sessionid (server side) to retrieve gamedata based on that sessionid. The login page for this, is done with a certain framework (cake), wich we are not going to change, because the existing game is already in production phase.

The problem:
The original form sends the username and password with nested variables like this:


<input id="LoginUsername" type="text" value="" maxlength="40" name="data[Login][username]"/>
<input type="password" id="LoginPassword" value="" name="data[Login][password]"/>


AS3
Trying to replicate the request in as3 like this:

public function login(username:String, password:String):void{
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(this.baseUrl + this.loginPath);
var variables:URLVariables = new URLVariables();
variables.data['Login']['username']=username;
variables.data['Login']['password']=password;
request.method = URLRequestMethod.POST;
request.data = variables;
loader.load(request);
loader.addEventListener(Event.COMPLETE, onLogin);
}

but ofcourse, this isn't working, giving me the error on the variables.data line

TypeError: Error #1010: A term is undefined and has no properties.
wich is understandable... because variables.data doesn't have properties, but i have no idea how to do it the right way...

someone has to clear my mind on this, its killing me :h:

.ral:cr
July 21st, 2009, 11:03 AM
but what is data?

after the variables word you should simply put the variable name that you'll use further in php script

variables.username = username;

kipty
July 21st, 2009, 11:05 AM
would you do it something like this, where username and password are properties of the variables object


public function login(username:String, password:String):void{
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(this.baseUrl + this.loginPath);
var variables:URLVariables = new URLVariables();
variables.username=username;
variables.password=password;
request.method = URLRequestMethod.POST;
request.data = variables;
loader.load(request);
loader.addEventListener(Event.COMPLETE, onLogin);
}

RvGaTe
July 21st, 2009, 11:10 AM
if the form would be using a non-array name like this:

<input id="LoginUsername" type="text" value="" maxlength="40" name="username"/>
<input type="password" id="LoginPassword" value="" name="password"/>

then i would use (like you guys states)


public function login(username:String, password:String):void{
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(this.baseUrl + this.loginPath);
var variables:URLVariables = new URLVariables();
variables.username=username;
variables.password=password;
request.method = URLRequestMethod.POST;
request.data = variables;
loader.load(request);
loader.addEventListener(Event.COMPLETE, onLogin);
}


but, in my original post... i stated that the form names are not simple single words... but instead the property added to variables should be: data[Login][password] (thats the name of the input fields)... how would i accomplish that?

kipty
July 21st, 2009, 11:28 AM
so in your form what is data[Login][password], a multi dimensional array?

RvGaTe
July 21st, 2009, 11:34 AM
data[Login][password] is just the name of the input field... just like any other name you would use on any other form element... but when using brackets in the name, php processes this as array's

kipty
July 21st, 2009, 12:15 PM
not sure as you know you cant name the property of the object 'data[login][password]'

If the php is just looking for a property of the variables object called data[login][password] could you not call the property something else e.g. data_Login_password or something and then before the data is sent to the PHP you are currently using, write a function that recieves the variables object to convert it to the format you require.

Krilnon
July 21st, 2009, 07:52 PM
It's legal to do something like this:
variables['data[Login][username]'] = username;
variables['data[Login][password]'] = password;

RvGaTe
July 22nd, 2009, 05:57 AM
It's legal to do something like this:
variables['data[Login][username]'] = username;
variables['data[Login][password]'] = password;

thank you very much, this worked like a charm (good to see you're still around to!)

Krilnon
July 22nd, 2009, 07:13 PM
(good to see you're still around to!)

:beam: Yep! Glad that it worked.