A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Retrieving variables from PHP.

  1. #1
    Senior Member MagnusVS's Avatar
    Join Date
    May 2005
    Location
    Oslo, Norway
    Posts
    764

    Retrieving variables from PHP.

    Hi! I've just started to learn AS3 and I found out that I had to use some new methods to retrieve data from PHP. And now (of course) it doesn't work

    My AS3 code looks like this:
    //Create the URLLoader instance
    var myLoader:URLLoader = new URLLoader()

    //the data will come as URL-encoded variables
    myLoader.dataFormat = URLLoaderDataFormat.VARIABLES

    //Load using an URLRequest, even beeing local
    myLoader.load(new URLRequest("http://magnusvs.net/flashgalleri/connectToDb.php"))

    //onLoad handler listener
    myLoader.addEventListener(Event.COMPLETE, onDataLoad)

    //add a listener for the complete event
    function onDataLoad(evt:Event){

    if(evt.target.data!=""){
    trace(evt.target.data["id0"]);
    }
    }
    The structure of the string that the php code echoes is like this:
    &id0=1&navn=asdasd&id1=1&navn=dffhr
    When I run the AS3 code, I get this error code:
    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:nComplete()
    If I remove
    myLoader.dataFormat = URLLoaderDataFormat.VARIABLES
    and just trace
    evt.target.data
    (not a specific variable), I get the entire output, but just as a string ofcourse...

    Does anybodt know how I can retrieve separate variables/being able to loop through the output variable by variable?

  2. #2
    Actionscript Developer KigD's Avatar
    Join Date
    Jan 2003
    Location
    georgia
    Posts
    597
    Quote Originally Posted by MagnusVS
    Hi! I've just started to learn AS3 and I found out that I had to use some new methods to retrieve data from PHP. And now (of course) it doesn't work

    My AS3 code looks like this:


    The structure of the string that the php code echoes is like this:


    When I run the AS3 code, I get this error code:


    If I remove and just trace (not a specific variable), I get the entire output, but just as a string ofcourse...

    Does anybodt know how I can retrieve separate variables/being able to loop through the output variable by variable?

    This error happens to everyone. Happened to me too. Basically you have to remove the & from the FIRST variable that's outputed.
    K2xL - My games, tutorials, message boards, and experiments.
    Blog

  3. #3
    Senior Member MagnusVS's Avatar
    Join Date
    May 2005
    Location
    Oslo, Norway
    Posts
    764
    Thanks for replying!

    I still get an error, as long as I include:
    myLoader.dataFormat = URLLoaderDataFormat.VARIABLES

    If I remove it, I manage to trace the entire string that's printed, but not access single variables. Any thought about why?

    Here is the output from the database:
    http://magnusvs.net/flashgalleri/connectToDb.php

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Senior Member MagnusVS's Avatar
    Join Date
    May 2005
    Location
    Oslo, Norway
    Posts
    764
    Thanks! By modifying the example from the Flash documentation, I managed to load variables from a .txt file, using this code:

    var request:URLRequest = new URLRequest("http://www.magnusvs.net/flashgalleri/test.txt");
    var loader:URLLoader = new URLLoader();
    loader.load(request);
    loader.addEventListener(Event.COMPLETE, completeHandler)
    function completeHandler(event:Event):void
    {
    var variables:URLVariables = new URLVariables(loader.data);
    trace(variables.monthNames);
    }
    But when I tried to load a .php file, I get the same error as earlier, even though I print exactly the same text as in the .txt file.

    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 connectToDb_fla::MainTimeline/completeHandler()
    at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/flash.net:URLLoader:nComplete()
    Any suggestions on why this happens?

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    As long as you test on your harddrive you will get this error, but on the server it will work, just neglect it.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Senior Member MagnusVS's Avatar
    Join Date
    May 2005
    Location
    Oslo, Norway
    Posts
    764
    Jey! It works
    Thank you both for taking time to answer my questions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center