Simple Flex & PHP Example
Line Break
Author: Arjan (47 Articles) - Author Website
Arjan is a SAP Consultant specialized in ABAP and Front End development techniques like Web Dynpro, Adobe Interactive Forms, Flex and AIR. In his free time he likes to create examples for Flex-Blog and other applications using Flex, AIR and PHP. Other hobbies are movies and music. He is also the co-owner of Flex-Blog.com.
How do i get my data from Flex to PHP? I wondered about this a year or so ago when I was just getting to know Flex. I have to admit that my PHP skills are not that good, I can get things in and out of MySQL databases and that’s about it. But after figuring it out, I wanted to share. Transferring data can be a mammoth task, and I think we all know that. So to have a quick tutorial allowing you to avoid the traumatic experience of a failed data transfer. Just follow the instructions, and you could repeat the process for anything from your personal blog to o2.co.uk/. I hope it helps, and I know that I found this amazingly useful when I first discovered it. But enough about me, let’s make a simple Flex application that calls some PHP script on the server.
Ok to keep it simple i’m just going to create a function that concatenates two string that are filled in in the Flex application and the result is send back.
First we need a screen with two TextInput fields, One Text output field and a Button to trigger the application to call the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <mx:VBox top="10" left="10"> <mx:HBox verticalAlign="middle"> <mx:Label text="String 1:"/> <mx:TextInput id="stringOne"/> </mx:HBox> <mx:HBox verticalAlign="middle"> <mx:Label text="String 2:"/> <mx:TextInput id="stringTwo"/> </mx:HBox> <mx:HRule width="100%"/> <mx:Button label="Concatenate!" click=""/> <mx:Text text=""/> </mx:VBox> |
The click event of the Button doesn’t do anything yet, and the output text is not determined yet. We will get to that later.
Ok let’s do some PHP. Put the following PHP script on your server somewere, let’s call it concat.php:
1 2 3 4 5 6 7 8 | <?php $stringOne = $_POST['stringOne']; $stringTwo = $_POST['stringTwo']; print $stringOne . $stringTwo; ?> |
Now to call that script, we are going to use the HTTPService component. We have to specify an id, url, message and a resultFormat. Furthermore, we have to specify the request inside the HTTPService tag:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <mx:HTTPService id="concat" url="concat.php" resultFormat="text" method="POST"> <mx:request xmlns=""> <stringOne>{stringOne.text}</stringOne> <stringTwo>{stringTwo.text}</stringTwo> </mx:request> </mx:HTTPService> |
The tag names inside the request are very important: they must match the post variables that you’re expecting in your PHP script, and they are ofcourse, case sensitive.
The values of the two input fields appear inside the stringOne and stringTwo tags, using a binding expressing on the text property of the component.
Ok now all we need to do is adding some code to the Button we created earlier. This code will trigger the application to send the HTTPService request to the server:
1 | <mx:Button label="Concatenate!" click="concat.send()"/> |
Finally, add code to the Text component at the bottom of your application to show the result. We can achieve this by binding the text property to the lastResult property of the HTTPService.
1 | <mx:Text fontSize="14" text="{concat.lastResult}"/> |
Ok, when we put it all together it should look something like the following example, use right mouseclick > view source to view the complete sourcecode.
Related posts:
Comments
3 Responses to “Simple Flex & PHP Example”



(
Hi
you have write great topic for starting flex, its first time i am writing feedback, thanks for publishing nice tipic
Hi Arjan,
I want to make report my project by Flex, data is get from database. Do I create it with Flex? like that http://flex.stimulsoft.com/DemoFx_Flex.html
Thank
i want to make blog in php how do i do that….