We help IT Professionals succeed at work.

Loading in variables from php script flash as3

Alex500
Alex500 asked
on
Hey, Im working on a script that loads variables from a php page into a flash document and I cant seem to get it to work. Eventually i will need the figure  as a variable so i can pass it to movieclips that will animate.

my flash as3 code is as follows

package {
      
      import flash.display.Sprite;
    import flash.events.MouseEvent;
      import flash.events.*;
      import flash.display.*;
      import flash.net.URLRequest;
      import flash.display.Loader;
      import flash.events.ProgressEvent;
      import flash.net.URLVariables;
      import flash.net.URLLoader;
      import flash.net.URLRequestMethod;
      import flash.net.URLLoaderDataFormat;
      import flash.net.URLRequest;
      import flash.net.navigateToURL;
      
      
      
      
      
      public class quizResults extends MovieClip {
            
            
            
                        private var england:englandText_mc = new englandText_mc();
      
      
                        private var theRecord:String;
                        private var loadData:URLLoader = new URLLoader();
                        
      
                  
                  
                  public function quizResults() {
                  
                  
                              stage.addChild(england);
                              england.x = 200;
                              england.y = 200;
                              getData();
                  
                  
                  }            
                  
                  
                  
                  
                  function getData() {
                        //theRecord = loadthis.text;
                        var variables:URLVariables = new URLVariables();
                        var varSend:URLRequest = new URLRequest("http://localhost:8888/FlashDev/facebook-version/new/Quiz/loadScore.php");
                        varSend.method = URLRequestMethod.POST;
                        varSend.data = variables;
                        var varLoader:URLLoader = new URLLoader;
                        varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
                        varLoader.addEventListener(Event.COMPLETE, showResults);
                        varLoader.load(varSend);
                  }
                  
                  
                  
                  function showResults (event:Event): void {
                        
                        if (event.target.data.msg == "Ok") {
                              england.text = event.target.data.englandVar;
                              trace("YES ALEX YOU WIN!");
                        }      else {
                              
                              trace("ALEX YOU FAIL AT LIFE");
                              
                        }
                        
                        
                        
                  }
      
                        
      
      
      }
      
}

and this is my php script:

$englandQuery = mysql_query("SELECT * FROM main WHERE country ='England'");

$gamesPlayedEngland = 0;
$totalScoreEngland = 0;


while ($england = mysql_fetch_array($englandQuery)) {
                  $gamesPlayedEngland++;
                  $totalScoreEngland += $england['score'];      
}

            echo "Average Score " ;
            echo $totalScoreEngland/$gamesPlayedEngland;
            $resultEngland = $totalScoreEngland/$gamesPlayedEngland;
            
            $englandVar = $resultEngland;
            
            echo "<br />";
            echo "<br />";
            
            if ($englandVar != 0) {
                  $output .= "msg=Ok";
                  $output .= "&score=" . $englandVar;      
                  echo $englandVar;
            } else {
                  $output = "msg=Error";
            }
            
            
            echo $output;


      
the php works fine as i have tested this and i dont get any errors in my script when i compile the swf. however when i load the swf i get my "alex you fail at life" message and i cant work out why.

Cheers



Comment
Watch Question

to use  the VARIABLE dataformat your return value from php should be in strict format.

right now you are echoing a lot of extra stuff

your $output variable is perfectly ok. just that you are echoing other stuff before that such as "<br/>" and the average score.. dont print or echo anything other than the output.. dont even leave any extra line breaks outside php tags. and it will work fine.

your output should only contain
msg=ok&score=SCORE

but right now it contains
Average score SCORE<br/><br/>msg=OK&score=SCORE

which is not a proper variable format

good luck

Author

Commented:
Cheers mate that has worked for me but now I get this error message.

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()
      at flash.net::URLLoader/onComplete()

I'm stumped!

Author

Commented:
sorry this is the error

ReferenceError: Error #1069: Property msg not found on String and there is no default value.
      at quizResults/showResults()
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at flash.net::URLLoader/onComplete()
Top Expert 2009

Commented:
has a laugh at your trace msgs.
Glad its not just me but don't debase yourself you dont' suck at life, you just suck at what you're doing right now. And we all have those moments.
haha

if i may step in. i think the problem is this

event.target.data.msg

you're returning this from your php right --> msg=ok&score=something
the way i usually do this (and i might not be right) - is that flash doesn't recognise those as variables it's just  a string to flash, so you can do a string.split to pull out the "ok" part of the msg. I usually have my php return xml so its easier but if you dont' have much date the str bit should be fine.

Author

Commented:
blue-genie, cheers for your message. How would I go about implementing this in to my code? I know XML is better I probably should have gone down that route in the first place but I've got so far developing it this way I'm going to keep it that way.
Top Expert 2009
Commented:
do something along these lines.

assuming that event.target.data.msg is going tor return

msg=ok&score=50;


function showResults (event:Event): void {
                        var resultStr:String = event.target.data.msg.toString();
                        var arr:Array = resultStr.split("&");

                        if (arr[0] == "msg=ok") {//if you want to be really specific you can split it again on the "="
                              england.text = event.target.data.englandVar;
                              trace("YES ALEX YOU WIN!");
                        }      else {
                              
                              trace("ALEX YOU FAIL AT LIFE");
                              
                        }
                        
                        
                        
                  }

Open in new window

Author

Commented:
I still get the same error messaging even after I have put in the new code. This is very frustrating.

Author

Commented:
didnt work at first but after a bit of tinkering it worked really well, cheers for the help!
Top Expert 2009

Commented:
what did you tinker?