jump to navigation

Read and write data to PHP with Flex June 12, 2010

Posted by Tournas Dimitrios in Flex and PHP.

This is a small sample of how to read and write data to and from PHP with Flex.This article shows some small sample code to transfer data using XML.
Notice I have created a database and table each named test, and the table has 3 columns testid, name, and email. You’ll have to create that database and that table for this to work. Name this file rest.php.

//connect to mysql
$MySQLConnection = mysql_connect( "localhost", "root", "root" );
//load the database
mysql_select_db( "test" );
//get information from the test table
$Query = "SELECT * from test";
$Result = mysql_query( $Query );
/* print out your own XML */
print "<people>\n";
while( $Row = mysql_fetch_object( $Result ) )
   print "<person><testid>".$Row->testid."</testid><name>".$Row-&gt;name."</name><email>".$Row-&gt;email."</email></person>\n";
print "</people>";

Here is the Flex front end code:

xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationcomplete="rest_service.send()">
    httpservice id="rest_service" url="http://localhost/rest.php">
    datagrid left="0" right="0" top="0" bottom="0" dataprovider="{rest_service.lastResult.people.person}">
          datagridcolumn headertext="Test ID" datafield="testid">
            datagridcolumn headertext="Name" datafield="name">
            <mx:datagridcolumn headertext="Email" datafield="email"></mx:datagridcolumn>

Two things to notice:
The dataProvider attribute of the DataGrid is simply traversing the XML tree. You need to be on the attribute that repeats (in this case person), and not its parent.
Notice that the DataGridColumn’s dataField attribute match the column names in the database table.
Writing data to the server.
PHP code, named write.php:

$Text = "First is ".$_GET["first"];
$Text .= "\nSecond is ".$_GET["second"];
file_put_contents( "data.xml", $Text );
//you could easily write code here that will update data in a database

The Flex code:

xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:httpservice id="writeData" url="”http://localhost/write.php”" method="GET" resultformat="text" result="resultHandler(event)"></mx:httpservice>
            import mx.rpc.events.ResultEvent;
            import mx.controls.Alert;
            public function resultHandler( event: ResultEvent ):void
                Alert.show( "Result "+String(event.result) );
            public function sendData( ):void
                var objSend:Object = new Object;
                objSend.first = textInput.text;
                objSend.second = "Second Text";
                writeData.send( objSend );
 textinput x="36" y="54" id="textInput">
    <mx:button x="204" y="54" label="Send" click="sendData();"></mx:button>

Notice that the method on the HTTPService is set to GET, and the PHP code is reading in values from the GET global variable.  You could also change the method to POST, and then change the PHP script to read from the POST variable


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s