Flex PHP simple communication instance

Source: Internet
Author: User

Flex is very powerful, but if you want flex to operate the database, you must use other scripting languages, such as PHP or Asp.net. I tried to communicate with PHP using Flex and the test was successful. At the beginning, I searched for other people's experience on the Internet, but I couldn't debug it on my own. There were various and inexplicable errors. Then, refer to the official flex documentation and examples. In the flex manual, search for "httpservice", which has the following description: Use the <mx: httpservice> label in the mxml file to represent the httpservice object. When the send () method of the httpservice object is called, an HTTP request to the specified URL is sent and an HTTP response is returned. You can choose to pass parameters to a specified URL. If the server-based proxy service is not used, you can only use the http get or POST method. If you set the useproxy attribute to true and use the server-based proxy service, you can also use the HTTP head, options, trace, and delete methods.

I have read the attributes and methods of httpservice in the manual, read the official instance source code, and I have written examples of communication between flex and PHP.ProgramSubmit a number from Flex to PhP. php first checks whether it is a number. If yes, multiply the number by 10 and return the result; if not, the Chinese prompt "not a number" is returned ".

Demonstration CLICK: Flex and PHP Communication

The source code is as follows:

Demo. mxmlSource code:

<? XML version = "1.0" encoding = "UTF-8"?> <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"> <mx: SCRIPT> <![CDATA [  Import  MX. rpc. Events. resultevent;  Import  MX. rpc. Events. faultevent;  Import  MX. Controls. Alert;  Public   Function  Handlexml (Event: resultevent): void {textarea1.text = Event. Result. tostring (); textinput2.text = Event. Result. Option. returndata ;} Public   Function  Handlefault (Event: faultevent): void {alert. Show (event. fault. faultstring, "Error" );}] > </MX: SCRIPT> <mx: httpservice id = "XMLRPC" URL = "Http://wangkangle.com/apps/flex_test.php" Result = "Handlexml (event )" Fault = "Handlefault (event )" Resultformat = "E4X"> <mx: Request> <data1> {textinput1.text} </data1> </MX: Request> </MX: httpservice> <mx: textinput x = "10" Y = "10" id = "textinput1"/> <mx: button x = "178" Y = "10" label = "button" id = "button1" Click = "XMLRPC. send () "/> <mx: textinput x =" 10 "Y =" 40 "id =" textinput2 "/> <mx: textarea x = "10" Y = "70" Height = "216" width = "233" id = "textarea1"/> </MX: Application>

 

The PHP source code (Demo. php) is as follows:

<? PHP  If ( Is_numeric ( $ _ Request ["Data1" ]) {  $ Returndata = $ _ Request ["Data1"] * 10 ;}  Else  {  $ Returndata = "Not a number" ;}  $ Results [] = "<Options>" ;  $ Results [] = "<Option> <returndata> ". $ Returndata . "</Returndata> </option>" ;  $ Results [] = "</Options>";  Print   Implode ("\ N ", $ Results  );  Exit  ; ?>

 

If the entered character is not a number, but in flex does not show "not a number", but shows garbled information, you can save the demo. php file as a UTF-8 format to see the correct Chinese. In httpservice, you can also add the method attribute. The value can be set to post or get, and the default value is get. With this attribute added, $ _ request ["data1"] in PHP can be written as $ _ Get ["data1"] or $ _ post ["data1"]

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.