This is a very basic example which makes use of Flex 3 in the frontend , MySQL for data storage and PHP to glue Flex and MySQL. There are two source files used in this example:
i. flex.mxml - for the interface part
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" creationComplete="userRequest.send()">
<mx:HTTPService id="userRequest" url="http://localhost/flex/request.php" useProxy="false" method="POST">
<mx:request xmlns="">
<username>{username.text}</username><emailaddress>{emailaddress.text}</emailaddress>
</mx:request>
</mx:HTTPService>
<mx:Form x="22" y="10" width="493">
<mx:HBox>
<mx:Label text="Username"/>
<mx:TextInput id="username" />
</mx:HBox>
<mx:HBox>
<mx:Label text="Email Address"/>
<mx:TextInput id="emailaddress" />
</mx:HBox>
<mx:Button label="Submit" click="userRequest.send()"/>
</mx:Form>
<mx:DataGrid id="dgUserRequest" x="22" y="128" dataProvider="{userRequest.lastResult.users.user}">
<mx:columns>
<mx:DataGridColumn headerText="User ID" dataField="userid"/>
<mx:DataGridColumn headerText="User Name" dataField="username"/>
</mx:columns>
</mx:DataGrid>
<mx:TextInput x="22" y="292" id="selectedemailaddress" text="{dgUserRequest.selectedItem.emailaddress}"/>
</mx:Application>
ii. request.php- for computation and communication with the mysql database.
<?php
Define( 'DATABASE_NAME', 'sample');
//connect to the database
$mysql = mysql_connect('localhost', 'root', '')
or die("no db connection");
mysql_select_db( DATABASE_NAME )
or die("no db selection");
if( $_POST["emailaddress"] AND $_POST["username"])
{
//add the user
$Query = "INSERT INTO users VALUES ('', '".$_POST['username']."', '".$_POST['emailaddress']."')";
$Result = mysql_query($Query);
}
//return a list of all the users
$Query = "SELECT * from users";
$Result = mysql_query( $Query );
$Return = "<users>";
while ( $User = mysql_fetch_object( $Result ) )
{
$Return .= "<user><userid>".$User->userid."</userid><username>".$User->username."</username><emailaddress>".$User->emailaddress."</ emailaddress></user>";
}
$Return .= "</users>";
mysql_free_result( $Result );
print ($Return)
?>
The above scripts assumes that you have already created a database, OR just follow the instructions below:
1. Create a database named 'sample' in mysql database.Then Create a table named 'users'.Create three fields : userid, username, emailaddress.
2. Copy the request.php file in localhost/flex directory.
3.In eclipse install flex builder. Then create a new project named flex . Then give other valuses as default while creating the project. Copy the contents of flex.mxml included in this pack into your eclipse working project.
4. Now run your project. You can see the output.
i. flex.mxml - for the interface part
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute" creationComplete="userRequest.send()">
<mx:HTTPService id="userRequest" url="http://localhost/flex/request.php" useProxy="false" method="POST">
<mx:request xmlns="">
<username>{username.text}</username><emailaddress>{emailaddress.text}</emailaddress>
</mx:request>
</mx:HTTPService>
<mx:Form x="22" y="10" width="493">
<mx:HBox>
<mx:Label text="Username"/>
<mx:TextInput id="username" />
</mx:HBox>
<mx:HBox>
<mx:Label text="Email Address"/>
<mx:TextInput id="emailaddress" />
</mx:HBox>
<mx:Button label="Submit" click="userRequest.send()"/>
</mx:Form>
<mx:DataGrid id="dgUserRequest" x="22" y="128" dataProvider="{userRequest.lastResult.users.user}">
<mx:columns>
<mx:DataGridColumn headerText="User ID" dataField="userid"/>
<mx:DataGridColumn headerText="User Name" dataField="username"/>
</mx:columns>
</mx:DataGrid>
<mx:TextInput x="22" y="292" id="selectedemailaddress" text="{dgUserRequest.selectedItem.emailaddress}"/>
</mx:Application>
ii. request.php- for computation and communication with the mysql database.
<?php
Define( 'DATABASE_NAME', 'sample');
//connect to the database
$mysql = mysql_connect('localhost', 'root', '')
or die("no db connection");
mysql_select_db( DATABASE_NAME )
or die("no db selection");
if( $_POST["emailaddress"] AND $_POST["username"])
{
//add the user
$Query = "INSERT INTO users VALUES ('', '".$_POST['username']."', '".$_POST['emailaddress']."')";
$Result = mysql_query($Query);
}
//return a list of all the users
$Query = "SELECT * from users";
$Result = mysql_query( $Query );
$Return = "<users>";
while ( $User = mysql_fetch_object( $Result ) )
{
$Return .= "<user><userid>".$User->userid."</userid><username>".$User->username."</username><emailaddress>".$User->emailaddress."</ emailaddress></user>";
}
$Return .= "</users>";
mysql_free_result( $Result );
print ($Return)
?>
The above scripts assumes that you have already created a database, OR just follow the instructions below:
1. Create a database named 'sample' in mysql database.Then Create a table named 'users'.Create three fields : userid, username, emailaddress.
2. Copy the request.php file in localhost/flex directory.
3.In eclipse install flex builder. Then create a new project named flex . Then give other valuses as default while creating the project. Copy the contents of flex.mxml included in this pack into your eclipse working project.
4. Now run your project. You can see the output.
Comments