7 Replies Latest reply on Jan 27, 2010 5:09 AM by Learningflex003

    Unable to load combobox using httpservice

    Learningflex003

      Hello Everybody,

       

      I been trying to load a ComboBox using httpservice for  the last 3 days with no success. After many research and video tutorials I still cant seem to get it to work..On one had i can load a datagrid but for some reason no go with the combo box.

       

      The data is loading based on the datagrid which seem to work.

      What am i doing wrong.

       

      Any help you can provide would be greatly appreciated.

       

      Thanks everybody

       

      My code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="778" creationComplete="initApp()">
      <mx:Script>
        <![CDATA[
         import mx.collections.XMLListCollection;
         import mx.controls.Alert;
         import mx.rpc.events.ResultEvent; 
       
         [Bindable]
         private var locationinfo:XMLListCollection = new XMLListCollection();
         [Bindable]
         private var cbx1coll:XMLListCollection;  
              
         private function initApp():void
         { dropdownBox.send();
             dropdownBox2.send();
         }
        
         private function ResultHandler(event:ResultEvent):void
         {  trace("test1");
            var result1:XMLList = event.result.location as XMLList;
           locationinfo = new XMLListCollection(result1);
           cbx1coll = new XMLListCollection(locationinfo.getItemAt(0).province);
           Alert.show("resulthand", "Alert Box", Alert.OK);
         }
        ]]>
      </mx:Script>

      <mx:HTTPService id="dropdownBox" url="http://localhost/Learning/inc/locationinfo.php"
         useProxy="false"  method="POST" result="ResultHandler(event)" resultFormat="e4x"/>
       

      // this part of the code works
      <mx:HTTPService id="dropdownBox2" url="http://localhost/Learning/inc/locationinfo.php"
         useProxy="false"  method="POST"  />
        
      <mx:HBox x="19" y="10" width="227" height="25" fontSize="8">
        <mx:Label text="Province/State:" width="111" fontSize="10" fontWeight="bold"/>
        <mx:ComboBox id="userProvinceState" dataProvider="{cbx1coll}" labelField="Province" ></mx:ComboBox>
      </mx:HBox>


      // this part of the code works

      <mx:DataGrid x="31" y="54" dataProvider="{dropdownBox2.lastResult.locations.location}"  id="grid1">
         <mx:columns> <mx:DataGridColumn headerText="province" dataField="prov"/> </mx:columns>
      </mx:DataGrid> 

      </mx:Application>

       

      part of the PHP file which might help with troubleshooting:

       

      print "<?xml version=\"1.0\"?>\n\n";

      print "<locations>\n";

      while($row = mysqli_fetch_array($GetProvince_query))

      { print "<location>";

      print "<prov>". $row['province'] ."</prov>";

      print "</location>";

      }

      print "</locations>\n";

       

        • 1. Re: Unable to load combobox using httpservice
          Flex harUI Adobe Employee

          Try labelField="prov"

          • 2. Re: Unable to load combobox using httpservice
            Learningflex003 Level 1

            Hello flexharul,

             

            I tried prov and still not able to get it to populate.

             

            thanks for your help..

             

            Still having working.

            • 3. Re: Unable to load combobox using httpservice
              Gregory Lafrance Level 6

              This works, but then there is only one item in the ComboBox:

               

                 private function ResultHandler(event:ResultEvent):void
                 {  trace("test1"); 
                    var result1:XMLList = event.result..location as XMLList;
                   locationinfo = new XMLListCollection(result1);
                   cbx1coll = new XMLListCollection(locationinfo.getItemAt(0).prov);
                   Alert.show("resulthand", "Alert Box", Alert.OK); 
                 } 
              


              Maybe you really want this, which returns all <prov>:

               

                 private function ResultHandler(event:ResultEvent):void
                 {  trace("test1"); 
                    var result1:XMLList = event.result..location as XMLList;
                   locationinfo = new XMLListCollection(result1);
                   cbx1coll = new XMLListCollection(locationinfo.copy().prov);
                   Alert.show("resulthand", "Alert Box", Alert.OK); 
                 } 
              

               

              If this post answers your question or helps, please mark it as such.


              Greg Lafrance - Flex 2 and 3 ACE certified

              www.ChikaraDev.com

              Flex / AIR Development, Training, and Support Services

              • 4. Re: Unable to load combobox using httpservice
                oldMster Level 3

                I think your problem is more basic.  In the datagrid you are referencing the 'location' level of the result, but in the result handler, you are referencing 'province', which doesn't appear to be an XML tag in your result at all.  Set cbx1 to .location, just like the datagrid, and I think you'll be in business.

                Mark

                • 5. Re: Unable to load combobox using httpservice
                  oldMster Level 3

                  Sorry, missed the first line of ResultHandler that set the xmllist to the .location of the result.

                   

                  But, why are you setting cbx1coll equal to only one row?  Don't you want cbx11coll to equal the 'location' level, and then reference the 'prov' data field in the combox box?  In any case, the .province property you are referencing in the resultHandler doesn't appear to exist in your xml - it would be the .prov property.

                   

                  Mark

                  • 6. Re: Unable to load combobox using httpservice
                    Learningflex003 Level 1

                    Hello Greg,

                     

                    I'm still not able to load the list of prinvices into the dorpdown combobox.

                     

                    Question, i have a Alert msg box the ResultHandler(event:ResultEvent) to get an indication the code was read but the alert msg box doesnt appear is this mean the resulthandler is being bypassed?.

                     

                    Stuck .lol

                    • 7. Re: Unable to load combobox using httpservice
                      Learningflex003 Level 1

                      Hello All,

                       

                      Just like to say thanks  for all of your help.

                       

                      I finally got it to work after many tries. Anyone whos looking for a working example here you go:

                      I included the flex app and the php script to play with.

                       

                      only thing I really didn't understand which was from a Adboe video tutorial is to use both xmllist and xmllistcollection but either case I got it to working.

                       

                      <mx:Script>
                              <![CDATA[
                                  import mx.collections.XMLListCollection;
                                  import mx.controls.Alert;
                                  import mx.rpc.events.ResultEvent;       
                                  import mx.rpc.events.FaultEvent;
                                  import mx.events.ListEvent;
                             
                                  [Bindable]
                                  private var locationinfo:XMLListCollection = new XMLListCollection();
                                  [Bindable]
                                  private var dropdownSelection1:String;
                                                                                 
                                  private function initApp():void
                                  {    dropBox.send();   
                                      userProvinceState.addEventListener(ListEvent.CHANGE, DropDownBox1Handler);           
                                  }   
                                 
                                  private function DropDownBox1Handler(event:ListEvent ):void
                                  {  dropdownSelection1 = event.currentTarget.selectedItem;               
                                  }
                                 
                                  private function runtest(event:ResultEvent):void
                                  {  var result1:XMLList = new XMLList();
                                     result1 = event.result.location.prov;
                                     locationinfo.source = result1;
                                  }
                             
                                  private function faultHandler(event:FaultEvent):void
                                  {    Alert.show(event.fault.faultString, event.fault.faultCode);               
                                  }
                              ]]>
                          </mx:Script>
                         
                          <mx:HTTPService id="dropBox" showBusyCursor="true" url="http://localhost/Learning/inc/locationinfo.php"
                               useProxy="false"  method="POST" result="runtest(event)" fault="faultHandler(event)" resultFormat="e4x"/>
                                  
                          <mx:Panel x="60" y="10" width="250" height="200" layout="absolute" title="DropDown List box example">
                              <mx:Label text="Province/State:" width="121" fontSize="12" fontWeight="bold" x="46" y="10"/>
                              <mx:ComboBox id="userProvinceState" prompt="Please select" dataProvider="{locationinfo}"  x="28" y="38" width="192"></mx:ComboBox>
                              <mx:Label x="46" y="85" text="{dropdownSelection1}" width="149"/>
                          </mx:Panel>
                         
                      </mx:Application>

                      ----------------------------

                      php script i used for my example

                       

                      <?PHP
                      require_once("connection.php");

                       

                      $Province_query = "SELECT DISTINCT province from tb_location";/
                      $GetProvince_query = mysqli_query($dbconnect, $Province_query) or die('<br/>Error querying Province:'.mysql_error());

                       

                      print "<?xml version=\"1.0\"?>";
                      print "<locations>";
                      while($row = mysqli_fetch_array($GetProvince_query))
                      {    print "<location>";
                              print "<prov>". $row['province'] . "</prov>";
                          print "</location>";
                      }
                      print "</locations>";

                       

                      mysqli_close($dbconnect);
                      ?>