in AS2 sending and receiving variables was easy, you could do it even with your eyes closed =D
but in AS3 is another story
remember AS2??
1 2 3 4 | var lv:LoadVars = new LoadVars(); lv.username = "ruliman"; lv.password = "123456"; lv.sendAndLoad("somefile.php",lv,"POST"); |
nice isn’t??
in AS3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | var variables:URLVariables = new URLVariables(); variables.username = "ruliman"; variables.password= "123456"; // trace(variables.toString()); var request:URLRequest = new URLRequest("somefile.php"); request.method = URLRequestMethod.POST; request.data = variables; var loader:URLLoader = new URLLoader(); loader.dataFormat = URLLoaderDataFormat.VARIABLES; try{ loader.load(request); } catch (error:Error) { trace("Unable to load URL"); } |
until here you can say “well it isn’t too bad just a bunch of classes, but is better organized =) i love AS3 ”
but……..when you compile Flash tell you this!
Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables$iinit()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()
MUAHAHAAA!!!
the PHP code is simple
1 2 3 4 | <? $user= $_POST["username "]; $pass= $_POST["password"]; ?> |
so…………where the error is coming from??
i google it and found this answer, is very simple:
my php wasn’t returning something so, put an echo throwing some variable something like:
1 2 3 4 5 6 | <? $user= $_POST["username "]; $pass= $_POST["password"]; echo "result=success"; ?> |
and that’s it everything compiles normal, now i can go back to dinner =D
16 Comments
So how would I do the as3 equivelant to the ‘andLoad’ part of sendAndLoad?
I tried loader.addEventListener(Event.COMPLETE, dataIn);
Need to get data back from the DB once its made the request, but no luck.
Ahh nevermind I figured it out
yep, is :
loader.addEventListener(Event.COMPLETE, onComplete_handler);
function onComplete_handler(event:Event) {
var vars:URLVariables = new URLVariables(event.target.data);
trace(vars);
}
you just need to pass all variables in the echo separated by “&”
cheers.
I did something similar. I made a new loader rather than new variables.
loader.addEventListener(Event.COMPLETE, onDataIn);
function onDataIn(e:Event)
{
var loader:URLLoader = URLLoader(e.target);
var result = loader.data.result;
//result would be what gets echo’d out [ie. result=success
}
I’m pleased to know grupow has a labs blog, this “tip” solves me some questions, thanks. What about using navigateToURL(request)?
Hi Wences! thanks for your comment
as far as i know navigateToURL is like getURL in AS2 but you could send variables via navigateToURL something like:
var url:String = “http://www.grupow.com”;
var variables:URLVariables = new URLVariables();
variables.sessionId = new Date().getTime();
variables.userLabel = “ruliman”;
var request:URLRequest = new URLRequest(url);
request.data = variables;
try {
navigateToURL(request);
}
catch (e:Error) {
// handle error here
}
but the URLLoader is the fancy way =D
cheers
hi all,
how can i get more than 1 variable. here, name -> result and value->success, but i want to get more than 1 variable.
actually i want a ComplexObject in response but i dont think it can happen in AS3 so what i m thinking to do is evaluating all the data in the ComplexObject and then form a query string but before i strated to do so I must know do I really able to pass a query string with more than 1 variable or not?
thanks
hi Haris
umm for a ComplexObject you need to switch to AMF
you can use AMFPHP is very easy to use
for receiving more than one variable in the query string you just need to separate all variables by a “&” (you can see URLVariables for more info)
in the php the echo would be something like :
echo “result=success&users=user1,user2,user3&importanVar=10&”;
you can see that users variable the value is “user1,user2,user3″;
back in AS3 you can split that string and access like an array
And how do I do to get the response in case the PHP file generates some content?
you mean the PHP is returning some variables?¿
Thank you very much. Very helpful!!!
Im sending strings to a IP control unit (WACI serial Nugget from Aurora multimedia) and it doesnt work in AS3. im sending the exact same thing in AS2 and 3 but it only works for 2. do you think it’s something in the WACI unit? dont get it though? if its sending the exact same message how would it even differentiate the 2
ummm…….. and the WACI unit is returning something??¿¿
thanks for posting that small but crucial piece of info regarding the “echo” portion of the php. thought i was losing my mind.
I am doing a project and my head is melted I can not get my login working without an error.Please can you help I have tried hundred of ways but still can not get it working.
I want to send back the userId from the login table as I use it for a test later in my flash site.
butSubmit.addEventListener(MouseEvent.CLICK, btnDown);
function btnDown(event:MouseEvent):void {
// Assign a variable name for our URLVariables object
var variables:URLVariables = new URLVariables();
// Build the varSend variable
var varSend:URLRequest = new URLRequest(”login.php”);
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
variables.userName = userName.text;
variables.userPassword= userPassword.text;
variables.sendRequest = “parse”;
varLoader.load(varSend);
// When the data comes back from PHP we display it here
function completeHandler(event:Event):void{
var loader:URLLoader = URLLoader(event.target);
var result = loader.data.result;
//if(result==success){
//MovieClip(parent).gotoAndStop(”test1″);
//}
//var phpVar1 = event.target.data.var1;
result1_txt.text = result;
}
now my php file one of many I HAVE TRIED
0){
echo “result=success”;
}
else
{
echo “result=failure”;
}
?>
// I was trying this method
while($row=mysql_fetch_array($result)){
$userId=urlencode($row['userId']);
$userName=urlencode($row['userName']);
echo “userId=$userId”;
echo “&username=”$userName”;
Hi Sheera!
i don´t know if is too late for this answer hehe but……
did you fix it??