Let's examine each line in detail: <?xml version="1.0" encoding="utf-8"?> These are the first two lines of each Flex application. The first line declares that this is an XML document. The second declares that this is an Application, provides the namespace for MX components, declares the layout to be "absolute" (meaning you can position items to the exact x and y coordinate. Other options are horizontal layouts or vertical layouts), and finally "creationComplete="userRequest.send()" says that on completion of loading the UI, call the function send() on the MXML element with the id userRequest. <mx:HTTPService id="userRequest" This is where we set up the HTTPService, to send and receive data from the PHP script we created. We set the id to userRequest, and provide a URL to our PHP script. We set the method of submit to POST (we could also use GET, but then we'd have to change our variables in the PHP script). The request itself contains two variables, username and emailaddress. The value for username is set to the text attribute of the element with id "username" (username.text) and the value for the PHP variable _POST["emailaddress"] is set to the text attribute of the element with id "emailaddress" (emailaddress.text). The { and } bind the variables to the value of the UI elements. Just to be clear, if we changed <username> to <user_name>, we would have to change our PHP variable to _POST["user_name"]. If we change {username.text} to {user_name.text}, we would have to modify our MXML: the element with the ID "username" would need to have its ID changed to "user_name." Next, we build a simple form: <mx:Form x="22" y="10" width="493"> Notice that we can lay out the exact x and y coordinates of the form, and set its exact width. Then, two HBoxes surround a label and textinput, allowing them to flow from left to right, one above the other. Finally, our Submit button appears at the end of our form. When the button is clicked, it calls the send() function of the element with ID "userRequest" (in this case, it is our HTTPService element). OK, so we've got the area that we submit new entries to the database, but where to we display them? That's next: <mx:DataGrid id="dgUserRequest" x="22" y="128" In this case, we have a DataGrid, which populates itself with the XML that we get from the userRequest HTTPService. We return an XML document, and in this case we bind the DataGrid to the user elements in the XML document that gets returned. The returning XML looks something like this: <users> Notice that we bind to the actual elements that get returned, not to the wrapper element around them. The DataGrid displays the userid and usernames of people in the database. I decided not to show the email address in the datagrid, but you could add another column with that information in it. Notice that the columnName needs to map directly to the XML elements. The DataGrid element will take care of allowing our users to sort and highlight the rows as they are selected - we don't need to do anything for that! Finally, we have a TextInput, which shows the email address of the selected user (dgUserRequest.selectedItem.emailaddress), and then an XML tag that closes the application. So, there you go. A simple Flash based application that submits and retrieves data from a MySQL database, using PHP as a back end. I urge you to download Flex Builder 2.0 and build more complicated applications using PHP, MySQL and Adobe Flex. Check out my blog at http://blogs.adobe.com/mikepotter/ for more information on Adobe Flex. And please provide suggestions for future articles and what other samples you'd like to see using this set of technologies.
blog comments powered by Disqus |
|
|
|
|
|
|
|