A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: AS3/PHP getData function problem

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Posts
    1

    AS3/PHP getData function problem

    Hi folks!

    I'm designing a flash app that sends and loads several types of data from a MySQL db using PHP.
    For the simplicity sake, I'm trying to avoid using remoting; instead I use URLVariables...
    The problem comes when I request some data using this getDBData function:

    Code:
    var:strDBData:String;
    
    function getDBData(datetime:Number, column:String):String {
    	var myData:URLRequest;
    	myData = new URLRequest("http://localhost/prototype/get.php");
    	myData.method = URLRequestMethod.POST;
    	var variables:URLVariables = new URLVariables();
    	variables.Datetime = datetime;
    	variables.Column = column;
    	myData.data = variables;
    	var loaderData:URLLoader = new URLLoader();
    	loaderData.dataFormat = URLLoaderDataFormat.TEXT;
    	loaderData.load(myData);
    	loaderData.addEventListener(Event.COMPLETE, DBDataLoaded);
    	trace("Trace #2")
    	return strDBData;
    }
    function DBDataLoaded(evt:Event):void {
    	strDBData = evt.target.data;
    	trace("Trace #1");
    }
    As you can see, the function should output strDBData. Nevertheless, since Flash runs the code asyncronously, when the function returns strDBData, it hasn't been yet defined by DBDataLoaded (in other words "Trace #2" shows up 3s. before "Trace #1".

    How could I delay the code so that it returns strDBData after it has been defined within DBDataLoaded? or, alternatively, how could I get "Trace #1" before "Trace #2"?

    Cheers,

    Iñaki

  2. #2
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    I'm a bit confused by what you wrote with respect to question 1.
    How could I delay the code so that it returns strDBData after it has been defined within DBDataLoaded?
    Are you saying that strDBData is not tracing correctly?

    With regards to question 2,
    alternatively, how could I get "Trace #1" before "Trace #2"?
    one way you could do this is
    1) add a listener in getDBData for a custom event MY_EVENT that executes function trace2 on event
    2) add a dispatchEvent in DBDataLoaded for MY_EVENT
    3) function trace2 does the "Trace #2"

    This way "Trace #2 will only happen once DBDataLoaded has run, and "Trace #1" will occur prior to "Trace #2"

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