Storing data as XMLThe second function deals with storing the new data - positions of the squares - in an xml file. Then we need to write the xml and store it on the server. We do that with a php script.First we declare some local variables which we need in this function. Those are LoadVars objects to send and receive data. Then we create another button function for the Save button similar as before using the Delegate class and a listener. Of course we also need to make sure that the user has entered a name in the textinput field. // // function to create new xml to store data // public function storeData ():Void { // // create LoadVars object // var lv:LoadVars; lv = new LoadVars (); var lvAnswer:LoadVars; lvAnswer = new LoadVars (); // // saving the new positions // var saveData:Object = new Object (); saveData.click = Delegate.create (this, saveDataFunction); function saveDataFunction () { if (this.name.text == "" || this.name.text.length<6) { this.messageField.text = "Enter name with at least 6 letters."; return this.name.text; }Now we create the xml file starting with a unique name for the file and xml object. We use the username for that. However, we need to cast the username as XML. Then we create the actual xml file starting with the xml declaration. All other elements follow as shown by the comments. // // create an XML document with a unique xml object name // cast the username as XML // xstore_name = XML (this.name.text); xstore_name = new XML (); // // xml declaration // xstore_name.xmlDecl = ""; // // create XML nodes // var element1:XMLNode = xstore_name.createElement ("game"); var element2:XMLNode = xstore_name.createElement ("square1"); // // add attributes for the x and y values // element2.attributes.y_value = this.square1._y; element2.attributes.x_value = this.square1._x; var element3:XMLNode = xstore_name.createElement ("square2"); element3.attributes.y_value = this.square2._y; element3.attributes.x_value = this.square2._x; var element4:XMLNode = xstore_name.createElement ("square3"); element4.attributes.y_value = this.square3._y; element4.attributes.x_value = this.square3._x; var element5:XMLNode = xstore_name.createElement ("square4"); element5.attributes.y_value = this.square4._y; element5.attributes.x_value = this.square4._x; // // place the new nodes into the XML tree // xstore_name.appendChild (element1); element1.appendChild (element2); element1.appendChild (element3); element1.appendChild (element4); element1.appendChild (element5); // // create XML text nodes // var textNode1:XMLNode = xstore_name.createTextNode ("square1"); var textNode2:XMLNode = xstore_name.createTextNode ("square2"); var textNode3:XMLNode = xstore_name.createTextNode ("square3"); var textNode4:XMLNode = xstore_name.createTextNode ("square4"); // // place the new nodes into the XML tree // element2.appendChild (textNode1); element3.appendChild (textNode2); element4.appendChild (textNode3); element5.appendChild (textNode4);When we have finished the xml file we want to send it to the server and by a php script store the data. We create a name for that, which contains also the exact path. The rest of the script deals with sending the data using the loadvars object. We also include a loadvars object to receive an answer when the php file is parsed to see that everythig went well. // // sending out the data for the php file // this.messageField.text = "saved"; var fName:String = "folder/"+this.name.text+".xml"; lv.fName = fName; lv.x_name = xstore_name; lv.action = "openfile"; lv.sendAndLoad ("shared.php", lvAnswer, "POST"); // // getting a message from the php script // lvAnswer.onLoad = function () { if (this.result == "Okay") { thisClip.messageField.text = "Data are stored."; } else { thisClip.messageField.text = this.errorMsg; } }; } this.saveBut.addEventListener ("click", saveData); } }On the next page we will examine the php file and here you can download all the files. |