0

EDIT - Looks like you cannot parse data from PHP without going through XML. Using JSON will be the best way. Source

I'm very new to Flex been using it for 2.5 days. My Flex application accesses a PHP script that calculates two numbers. The script works as it is but I want to retrieve multiple variables from the PHP script so I can have an answer for not only addition but also multiplcation etc...

I hope I have made some sense....

Flex:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark"
        title="Adding Numbers">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <s:HTTPService id="srvCalc" url="http://192.168.0.3/flex/flex-phpexample/calc.php"
                   resultFormat="text"
                   method="POST">

        <s:request xmlns="">
            <number1>{txtNumber1.text}</number1>
            <number2>{txtNumber2.text}</number2>
        </s:request>

    </s:HTTPService>
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            private function btnCalc_Click(event:MouseEvent):void
            {
                srvCalc.send();
            }
        ]]>
    </fx:Script>

    <s:VGroup width="400" horizontalCenter="0" verticalCenter="0">
        <s:Label id="txtAnswer" text="{srvCalc.lastResult}" />

        <s:Label text="First Number:" />
        <s:TextInput id="txtNumber1"
                     width="100%"/>

        <s:Label text="Second Number:"/>
        <s:TextInput id="txtNumber2"
                     width="100%"/>

        <s:Button id="btnCalc"
                  label="Calculate"
                  click="btnCalc_Click(event)"/>
    </s:VGroup>

</s:View>

PHP:

<?php
    $number1 = $_POST['number1'];
    $number2 = $_POST['number2'];

    print($number1 + $number2);
?>
Tristian
  • 65
  • 1
  • 6

1 Answers1

0

The easiest method would be to have php return a JSON encoded block and then use http://flexjson.sourceforge.net/ to decode it in flex. There are some other methods you can use, but this is the easiest and most structured way.

Kyros
  • 512
  • 2
  • 5
  • I have looked, my initial idea I want to build up from this is to create a login view. So that we can send a username, password and when successful I want to return a value from the PHP code like: $loginSuccessful = true; $firstName = "John"; $lastName = "Doe"' – Tristian Oct 31 '12 at 18:38
  • Right, so you would want something along the lines of $return = array(); $return['firstname']="John"; $return['status']=true; echo(json_encode($return)); – Kyros Oct 31 '12 at 19:41
  • You don't actually need to use a third party library to parse JSON in AS3 anymore. Adobe has a JSON class (with #parse() and #stringify() static methods) built right in now. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html – Josh Oct 31 '12 at 21:14