HomePHP Page 2 - Rich Internet Applications: Introduction to Adobe Flex and PHP
Integrating Flex and PHP - PHP
AJAX is popular for the creation of rich Internet applications, but there are good alternatives. This article will introduce you to Flex applications, rich Internet applications that run inside the Adobe Flash player.
OK, now that we have a good idea of what Adobe Flex is, let's see how we can build something. The first step is to download Flex Builder 2.0 from the Adobe labs site at http://labs.adobe.com. If you are familiar with Eclipse, and have it installed, you can install the plugin version of Flex Builder. If not, then you'll want to install the standalone version.
Once installed, we'll want to create a new Flex application. In this tutorial, we're going to keep the application simple. We will create an application that will read names and email addresses from a database and display them to the user. Our simple application will also allow users to add usernames and email addresses to the database.
To start, create the database that we need. I've called mine "sample," but you can call yours whatever you like. Next, create the table that will hold the user data. Here is the SQL structure for our table:
OK, now that we have our table, let's create the PHP script that will add users and export the XML that the Flex application will consume. It's relatively simple, only 25 lines of code or so. Note that the quote_smart function is a best practice, according to the PHP.Net website (http://www.php.net/manual/en/function.mysql- real-escape-string.php).
The PHP should be fairly self explanatory. The $_POST variable is populated with values from our Flex application, with two fields required: emailaddress and username. If both of those are filled out, then it adds the user to the database. After that, we return a list of users in XML format. Note that you cannot pass PHP variables to Flex applications directly, they must be encoded in XML first.
By abstracting the user interface from the data retrieval, it allows you to easily change how the data is displayed. For example, you could use this same PHP script to pass data to a mobile phone version of the same application. All you would need for that is to write the front end of the application; the back end PHP script would remain the same.