5 Replies Latest reply on Aug 26, 2009 5:24 AM by larry_schwartz

    HTTPService, ActionScript, and working with lastResult or Result


      I've searched for an answer for this on the web and this forum, and either I can't find it or I didn't understand what I found.


      Thanks in advance for any help.


      I need to work within ActionScript to setup an HTTPService to retrieve a single ID number in XML format, store that ID in a variable, then do various things with that ID.


      My example MXML and PHP pages are below.


      I can successfully run the HTTPService and process the Result event in a handler.


      However, I can't figure out how to get that single ID number into a variable for use by other ActionScript functions.


      If I try to work with lastResult within my getID() function (the function that builds/executes the HTTPService), I get null errors.


      If I try to assign the resulting ID to a "global" variable (strCurID) via httpMaxIDResultHandler() (the HTTPService result handler), I don't see how to get the variable properly populated before using it (notice how my trace statement in doDBHit() produces null).


      (Note! If you click the DBHit button multiple times, don't be fooled when you see "99" in the "Trace in controlling function".  That's the value from the *previous* click/code execution.  If the PHP was truly dynamic, you'd see that the ID in the "Trace in controlling function" is always one iteration behind the currently returned ID.)


      THIS IS http_get_id_test.mxml




      <?xml version="1.0" encoding="utf-8"?>


      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" layout="absolute">



                  import mx.rpc.http.HTTPService;
                  import mx.rpc.events.ResultEvent;
                  import mx.rpc.events.FaultEvent;
                  private var strCurID:String;
                  private function doDBHit():void{
                      trace("Trace in controlling function:  " + strCurID);
                  private function getID():void{
                      var httpMaxID:HTTPService = new HTTPService();
                      httpMaxID.url = "php_xml_result.php";//PUT IN THE FULLY QUALIFIED PATH TO THE FILE IF PLACING IN A DIFFERENT DIRECTORY
                      httpMaxID.addEventListener(ResultEvent.RESULT, httpMaxIDResultHandler);
                      //httpMaxID.addEventListener(FaultEvent.FAULT, httpMaxIDFaultHandler);
                      httpMaxID.method = "GET";
                      httpMaxID.resultFormat = "xml";


                  private function httpMaxIDResultHandler(event:ResultEvent):void{
                      var xmlTheList:XMLList;
                      var xmlTheXML:XML;
                      xmlTheList = event.result as XMLList;
                      xmlTheXML = new XML(event.result.toString());
                      trace("Trace in Result Handler:  " + xmlTheXML.children().child("max_id").text());
                      strCurID = xmlTheXML.children().child("max_id").text();




          <mx:Button id="btnTestDBHit" label="DBHit" click="doDBHit();"  x="489" y="10"/>





      THIS IS php_xml_result.php






      echo "<id_lookup><id_lookup_row><max_id>99</max_id></id_lookup_row></id_lookup>";