Flash AS3 Get PHP Variable Value implementation method

Source: Internet
Author: User
Tags addchild http request php file php code

HP Code (test1.php):

This form of HTML code is not recommended:

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title> test AS3 How to read the PHP variable value </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<body>
<?php

/**
* The disadvantage of using this method is that there is no output after the output statement of the as value (the Echo statement below).
* Workaround: You can add a variable to the end of the output statement, which is not required to be received in the AS3, for example:
* echo "&coun=". $country. " &pro= ". $province." &city= ";
*/
$country = "China <br/>";
$province = "Fujian";
AS3 Read is a Coun, Pro property value, the front of "&" cannot be missing, otherwise this property will not read
echo "&coun=". $country. " &pro= ". $province;
?>
<!--Note: The line and subsequent lines will also be AS3 as the value of the Pro property, so the line and subsequent lines are redundant-->
</body>

The recommended change to this form of pure PHP code:

The code is as follows Copy Code
<?php
$country = "China <br/>";
$province = "Fujian";
AS3 Read is a Coun, Pro property value, the front of "&" cannot be missing, otherwise this property will not read
echo "&coun=". $country. " &pro= ". $province;
?>

AS3 Code (TEST1.AS):

The code is as follows Copy Code

Package
{
Import Fl.controls.Button;

Import Flash.display.Sprite;
Import flash.events.Event;
Import flash.events.MouseEvent;
Import Flash.net.URLLoader;
Import Flash.net.URLLoaderDataFormat;
Import Flash.net.URLRequest;
Import Flash.net.URLVariables;
Import Flash.text.TextField;
Import flash.text.TextFieldAutoSize;
Import Flash.text.TextFormat;

/**
* Test the interaction method between AS3 and PHP 1
* This method reads the value of the variable from the remote PHP load
* @author Administrator
*/
public class Test1 extends Sprite
{

/**
* Define Click button
*/
private Var _btn:button;

/**
* Text Box
*/
private Var _showtf:textfield;

/**
* Test the interaction between AS3 and PHP
*/
Public Function Test1 () {
_BTN = new Button ();
_btn.label = "please click";
_btn.move (0, 0);
AddChild (_BTN);

_SHOWTF = new TextField ();
_showtf.autosize = Textfieldautosize.left;
_showtf.defaulttextformat = new TextFormat (null, 0xff0000);
_showtf.wordwrap = true;
_showtf.border = true;
_showtf.text = "Content text box 1";
_showtf.width = 660;
_showtf.x = 110;
_SHOWTF.Y = 0;
AddChild (_SHOWTF);

_btn.addeventlistener (Mouseevent.click, loadphp);
}

/**
* Start loading PHP
* @param Event
*/
Private Function loadphp (event:mouseevent): void {
_showtf.text = "";

var urlloader:urlloader = new Urlloader ();
Urlloader.dataformat = Urlloaderdataformat.variables;

Note that this is used as an HTTP request as a loading address, not as a relative or absolute position, such as "/test1.php"
Urlloader.load (New URLRequest ("http://localhost:8081/as3php/test1.php"));
Urlloader.addeventlistener (Event.complete, Oncompletedhandle);
}

/**
* Load PHP complete
* @param Event
*/
Private Function Oncompletedhandle (event:event): void {
var mydata:urlvariables = Urlvariables (Event.currenttarget as Urlloader). data);
_showtf.text = "Country is:" + Mydata.coun + "province is:" + Mydata.pro;
}

}
}


Previous article AS3 get PHP variable value (1) to tell how to use AS3 to get the value of PHP variables, this article will continue to study this content to find out the nature of the PHP variable value in this way.

The PHP code used above is as follows:

The code is as follows Copy Code
<?php
$country = "China <br/>";
$province = "Fujian";
AS3 Read is a Coun, Pro property value, the front of "&" cannot be missing, otherwise this property will not read
echo "&coun=". $country. " &pro= ". $province;
?>

Access to this PHP file in the browser will be displayed on the browser:

&coun= China <br/>&pro= Fujian

See here, what do you think of? Isn't this just a simple Web page text? Yes, indeed, in order to verify our ideas, do the following tests:

Create a new HTML file, write "&coun= China <br/>&pro= Fujian", again using AS3 code to remotely load the HTML file, you will find the same as the previous loading of the PHP code results are identical. That explains a problem.--AS3 to get PHP variables in this way is to first parse the PHP file into an HTML file and then read the corresponding value from the HTML file. Therefore, this method not only can get the variable value of PHP, but also can be used to get JSP, ASP and other variables.

For example, the following JSP can also be used to obtain variable values in this way:

The code is as follows Copy Code

<%

String country = "China <br/>";
String province = "Fujian";
AS3 Read is a Coun, Pro property value, the front of "&" cannot be missing, otherwise this property will not read
Out.print ("&coun=" + Country + "&pro=" + province);

%>

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.