One of the main abilities of an Web application must be its communication with the server, the posibility to send data to the server and to receive a reply from it.
Flash, through ActionScript can send data to server and receive its response.
- In this tutorial it is presented the method to send XML data from ActionScript to a PHP script, with a Flash presentation (SWF).
// https://coursesweb.net/flash // define a Text field that will display the reply from the PHP var respTxt:TextField = new TextField(); // declare a variable that will store the reply from the PHP var re_server:XML; // Here you can load external XML content and edit it, then convert it into String, with toXMLString() // The String with the XML data var xml_str:String = '<?xml version="1.0" encoding="UTF-8"?><root><value>coursesweb.net</value></root>'; // Define an object with the URL of the PHP file, add the XML, and the method of transfer var php_url:URLRequest = new URLRequest("savexml.php"); // Object with the file's adress php_url.data = xml_str; // Add the XML string php_url.contentType = "text/xml"; // Define the type of the content php_url.method = URLRequestMethod.POST; // Sets transfer method - POST var obj_send_load:URLLoader = new URLLoader(); // the instance that will expedite the data // Register the Event listener that detects when the data was completely transferred and the reply is received obj_send_load.addEventListener(Event.COMPLETE, onComplete, false, 0, true); // Register the Event that is triggered in case of error in transfer obj_send_load.addEventListener(IOErrorEvent.IO_ERROR, onIOError, false, 0, true); // Function called by the event triggered when the transfer was finished function onComplete(evt:Event):void { // Uses try..catch to retain and customize possible errors try { re_server = XML(evt.target.data); // store in as XML object the reply from the PHP respTxt.text = re_server; // Add in the text field the server response } catch (err:TypeError) { // If any error is generated, it will retain it and include it in the text field respTxt.text = "Error when processing the reply from the server:\n" + err.message; } // Delete the event listeners, to free up memory removeEventListener(Event.COMPLETE, onComplete); removeEventListener(IOErrorEvent.IO_ERROR, onIOError); placeText(); // Calls the function that displays the text field } // Function called by the event triggered in case of a transfer error function onIOError(evt:IOErrorEvent):void { // captures the error, customizes it and adds it in the text field respTxt.text = "Error sending XML data.\n" + evt.text; placeText(); // Calls the function that displays the text field } // Function that displays the text field in the Flash presentation function placeText():void { // define properties for the text area (distance, multiline, alignment, border) respTxt.x = 5; respTxt.y = 15; respTxt.multiline = true; respTxt.autoSize = TextFieldAutoSize.LEFT; respTxt.border = true; addChild(respTxt); // Add the text field in Flash } // apply the load() method to expedite data (added in argument) obj_send_load.load(php_url);
<?php // https://coursesweb.net // sets tath and name of the XML file on the server $xml_file = 'xmls/test.xml'; // gets the data received through POST (in string format) $xml_data = $HTTP_RAW_POST_DATA; // If the data in $xml_data is saved on the server (in the location specified in $xml_file) // outputs them with a "echo" instruction // If data cannot be saved, returns an error if(file_put_contents($xml_file, $xml_data)) echo $xml_data; else echo 'Error, the XML data could not be saved on the server, at'. $xml_file; ?>
In ActionScript you can load and use an XML content from an external file, and process it before sending it to the PHP script.
It is important to convert the XML format into string before sending it.
- See the lessons from the ActionScript 3 section to learn about loading and processing XML data in Flash.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net