[Original] integration of flex and PHP scripts (1)-XML

Source: Internet
Author: User
ArticleDirectory
    • 1. system requirements:
    • 5. Use flex to create a user interface

For the latest version of this article, see: http://www.deepcast.net/wiki/ow.asp? Integration of flash and PHP

This article uses Adobe Flex
Builder2 and PHP create a small ApplicationProgramInstance, read the data including name and email address from the corresponding table of the MySQL database and display it to the user. You can also add a new name and email address to the database.

Note: Flex 2 beta 3. I from Mike Potter is available on the http://www.adobe.com/devnet/flex/articles/flex2_php.html website.
But there are someCodeIt may be a copy or a copy error, so after learning, I specially paste the code I have verified for your reference.

1. system requirements:

1. Install flex builder 2 (including the SDK) first)

2, PHP

3. MySQL and Apache (IIS is also supported)

Ii. Prerequisites:

Be familiar with simple php development and XML basics.

Iii. MySQL preparation
Create a data warehouse sampleon mysql, and then run the following SQL code sample.rar to create the table users.

4. php script

Then, you will begin to write the PHP script for adding users and outputting XML (for the flex program. This script is relatively simple, with only 25 rows. Note that,Quote_smart
The function is in line with PHP. net
Official Website specifications are used to verify user input security. Function as a best practice to help verify user input,
According to the php. net

The following is the getuser. php file in the Apache root directory:

<? PHP
Define ("database_server", "localhost"); // address of the Database Server
Define ("database_username", "root"); // database Login Name
Define ("database_password", "123456"); // database logon Password
Define ("database_name", "sample"); // Database Name

// Connect to the database

$ Mysql = mysql_connect (database_server, database_username, database_password );

Mysql_select_db (database_name );

// Verify whether the name and address entered by the user are safe

Function quote_smart ($ value)
{
// Stripslashes
If (get_magic_quotes_gpc ()){
$ Value = stripslashes ($ value );
}
// Quote if not integer
If (! Is_numeric ($ value )){
$ Value = "'". mysql_real_escape_string ($ value )."'";
}
Return $ value;
}

If ($ _ post ["emailaddress"] and $ _ post ["username"])
{
// Add the user
$ Query = sprintf ("insert into users values (null, % s, % s)", quote_smart ($ _ post ["username"]), quote_smart ($ _ 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)
?>

Note that $ _ postThe ["variable name"] format is used to represent the variables passed to the PHP script from the flex program. In the above example, it is transmitted in flex.EmailaddressAnd
Username. The user enters the above information in compliance with the verification security specificationsEmailaddressAnd
UsernameThen, PHP adds them to the database to add new users. Then, PHP will output a list of users in XML format.

You cannot directly pass variables from PHP to flex. You can first output the variables into XML, and then flex reads the XML to get the data returned, this eventually changes the data in the flex performance layer. Similarly, you can use the same PHP script as the background program to output data from PHP to the mobile phone. You only need to rewrite the front-end display.

So far, the involved PHP scripts and MySQL databases are old content. Then we will start to create the application interface.

5. Use flex to create a user interface

The flex application combines ActionScript 3.0 and mxml. ActionScript is based on ecmascript
(Similar to Javascript,
Therefore, it should be very familiar to Web developers. Mxml is the presentation layer of XML-based flex applications. Essentially, the layout of the user interface is defined in XML, and Script Programming is performed on the user interface using ActionScript. The mxml used in this article only has 26 lines of code:

<! -- The first line is the XML file declaration. -->
<? XML version = "1.0"
Encoding = "UTF-8"?>

<! --
The second line is the application declaration, which provides the namespace of the MX component. The layout is absolute positioning. Finally, it declares that the send () function of the object whose ID is userrequest -->
<Mx: Application
Xmlns: MX = "http://www.adobe.com/2006/mxml" xmlns = "*" layout = "absolute"
Creationcomplete = "userrequest. Send ()">

<! --
Set to send and receive data between the PHP scripts created by httpservice and ingress. Set httpservice ID to userrequest, set the URL of the PHP script file, set no proxy server, set the submission method to post, of course, you can also use get, but modify the variable name of the PHP script -->

<Mx: httpservice id = "userrequest" url = "http: // localhost/getuser. php"
Useproxy = "false" method = "Post">
<Mx: Request
Xmlns = "">

set the username value to the text attribute value of the element whose ID is username ( username. text )
, the variable _ post ["emailaddress"]
passed to PhP is also set to the value of the text attribute of the element whose ID is emailaddress (< code> emailaddress. text ). Braces bind the variable to the value of the interface element, that is, if you change to ,
the variable passed to PhP is changed to _ post ["user_name. text )
changed to {user_name.text} , you must modify In mxml to . -->
{username. text} {emailaddress. text}

Create a simple form, and click the button to call userrequest. Send. -->
width = "493">

text = "username"/>





click = "userrequest. send () "/>

<! -- Create a DataGrid component to display userrequest
The data retrieved by httpservice. Note that you must bind it to the user element instead of the users outsourcing element on the upper layer. The DataGrid component provides a convenient column sorting and highlighting function for the current row.
Finally, it is a text box that displays the corresponding email address dguserrequest. selecteditem. emailaddress to the current user. -->

<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>


The XML content retrieved from PHP is as follows:
<Users>
<User>
<Userid> 1 </userid>
<Username> Joe
Schmoe </username>
<Emailaddress> joe@schmoe.com </emailaddress>
</User>
<User>
<Userid> 2 </userid>
<Username> Betty
Schmoe </username>
<Emailaddress> betty@schmoe.com </emailaddress>
</User>
</Users>

The entire small FLASH application that uses the PHP background to add and retrieve Mysql Data is everywhere. All code files flexphp.rar can be downloaded here.

For the latest version of this article, see: http://www.deepcast.net/wiki/ow.asp? Integration of flash and PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.