views:

45

answers:

2

I'm trying to get variable importing from a PHP script before I implement it into a larger project. So far all I've gotten is headaches.

// this is all thats in test.php other than the open and close brackets.
// Normally I'd have it doing a mysql_query and putting useful information into the print statement.
print( "lamegame.net/test/test.php?val=foo&id=0000&name=Glenn");

All test.as has to do is access the three variables. The problem comes in that 'val' is undefined. The id and name variables however are just fine and return 0000 and Glenn respectively.

package {

    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.events.*;
    import flash.net.*;

    public class test extends MovieClip
    {
        public function test() 
        {
            super();
            //prep request
            //EDIT: sorry, I realize now it makes a bit of difference between test.php by itself and http://.../test.php.
            var request:URLRequest = new URLRequest("http://somewebsite.com/test.php");
            request.method = URLRequestMethod.GET;

            var loader:URLLoader = new URLLoader();

            //load request
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader.addEventListener(Event.COMPLETE, dataLoaded);
            loader.load(request);
        }

        public function dataLoaded(e:Event):void
        {
            var name = e.target.data.name;
            var id = e.target.data.id;
            var val = e.target.data.val;
            trace("Val: " + val + "ID: " + id + " Name: " + name);
        }
    }
}
+2  A: 

its mysterious to me how you can use a php parser in your flash ide. use a http:// link in your URLRequest. why dont you just use xml that gets build dynamically by php. its so much more readable. or you use swfobject and flash vars. then you just need to insert them in the javascript string.

antpaw
Ah I see, sorry about that. I'm going to blame lack of sleep for not typing out the string properly. It is an http://.../test.phpNot sure if that makes a huge difference though but I think I get what you're saying. I'll give the XML option a shot. I'm not to sure how I would try your other suggestion though? I need to check out Javascript.
Glenn
Tyler Egeto
Definitely looks like I'm missing out on some stuff here at school... Ok, well I'm going to go with XML for now. It looks like I need to urlencode() the string in test.php otherwise the <tag>'s are lost when it prints it out for test.as to grab. I assume I'll need to call decode in test.as or it will be called by the URLLoader automatically?
Glenn
Well now, it looks like I don't even need to do urlencode. I've got it working now, thank you all for the help!
Glenn
A: 

call me crazy but wouldnt it be much simpler just to have the php file spit out an xml feed for your swf to consume? xml is native in as3 you dont even have to parse it, just load it and youre good to go.

David Morrow
Yep. In the end that's what I ended up doing to get it working.As for sending variables from AS3 -> PHP, I ended up using post though. Seemed easier for some reason going that way.
Glenn