tags:

views:

466

answers:

2

I have a TextInput and a List in my application. I want to send the information written in TextInput and the names of the selected options from the list to a php file.

Following HTTPService sends the TextInput's text and the indices of selected items from list finely to a php file but the selectedItems is not working as i expected. It does not send the names of the selected items

<mx:HTTPService id="configureService" url="configure.php" resultFormat="text" method="POST">
    <mx:request xmlns="">
         <textInput>
            {textInput.text}
         </textInput>
         <selectedFoodNames>
            {foodList.selectedItems.join(",")}   <!-- Problem in this LOC -->
         </selectedFoodNames>
         <selectedFoodIndices>
            {foodList.selectedIndices.join(",")}
         </selectedFoodIndices>
    </mx:request>
</mx:HTTPService>

Now, my php file results:

echo $_POST['textInput'];  //Output the right answer
echo $_POST['selectedFoodNames']; //Outputs:  "[object Object],[object Object],[object Object]" if three items are selected from the list
echo $_POST['selectedFoodIndices']; //Outputs the indices of selected items separated by comma

The list look like:

<mx:List id="foodList" x="26.95" y="54" width="231.55" height="236.9" allowMultipleSelection="true">
    <mx:dataProvider>
     <mx:Array>
                <mx:Object id="Sugar" label="Sugar" data="#FF0000"/>
         <mx:Object id="Salt" label="Salt" data="#00FF00"/>
         <mx:Object id="Pepper" label="Pepper" data="#0000FF"/>
            </mx:Array>
    </mx:dataProvider>

Is there a way i can send the labels of the elements in list?

+1  A: 

You'll need to write a function to make a list from the objects.

public static function selectedItemsToLabels(list:List):String {
  var a:Array = [];
  for each(var o:Object in list.selectedItems) {
    a.push(o.label);
  }
  return a.join(",");
}

This is a static function but you can make it a member of a List if you want to extend that class.

Glenn
Right.okay i now saved the returned string as <br/>var temp:String = selectedItemsToLabels(algosList);<br/>but when using <selectedFoodNames>{temp}</selectedFoodNames> gives output error that temp in not defined. Please tell how do i send the contents of temp via HTTPService now?
baltusaj
No need for temp. I believe you can bind a function within <selectedFoodNames>, rather than a variable. Try that. I just use as3 for this kind of thing, so I might be wrong.
Glenn
A: 

I'm not sure if you want to get another framework involved, but I've used Zend AMF as a solution to such problems. It lets you pass Flex and PHP objects back and forth without having to manually create or parse XML intermediaries.

You can read more at: http://framework.zend.com/manual/en/zend.amf.html

davearchie