简体   繁体   中英

Passing Variables from Flash to PHP

Creating a flash project where users can visit the site, and turn off/on objects in a house (ie. lights, tv, computer, etc.) The next user who will visit the house in the website, will see what lights or house appliances were left on. Flash variables are passed to PHP, and those variables are saved in an XML file. (For testing to see what is being saved to the XML file, on each click --vars.xml opens.) In the vars.xml file, I see that the house objects that were last turned on--are saved in the XML file--BUT in the SWF file, ONLY one of the objects that are listed in the XML are turned ON. Only the last object that was clicked on would show ON--not all the objects in the XML file.)

package {
  import flash.display.MovieClip;
  import flash.events.MouseEvent;
  import flash.events.Event;
  import flash.text.*;
  import flash.net.*;

  public class House extends MovieClip {

    var request:URLRequest = new URLRequest("vars.xml")
    var loader:URLLoader = new URLLoader();


    // Constructor--------------------------------------------------------------------
    public function House()


    {

        loader.addEventListener(Event.COMPLETE, parseXML);
        loader.load(request); 

}

// function sendPhp ------------------------------------------------------------------
    function sendPhp():void
    {

        // send vars to php
        var request:URLRequest = new URLRequest("write_xml.php"); // the php file to send data to
        var variables:URLVariables = new URLVariables(); // create an array of POST vars


        for (var i:int=0; i<onList.length; i++) {
            variables["v"+i] = onList[i];
        }

        //variables['powerUsage'] = totalTxt.text;

        request.data = variables; // send the vars to the data property of the requested url (our php file)
        request.method = URLRequestMethod.POST; // use POST as the send method
        try
        {
            var sender:URLLoader = new URLLoader();
            sender.load(request); // load the php file and send it the variable data
            navigateToURL(new URLRequest("vars.xml"), '_blank'); //show me the xml
        } 
        catch (e:Error) 
        {
            trace(e); // trace error if there is a problem
        }
    }


// function parseXML ------------------------------------------------------------------

function parseXML(evt:Event)
{
    var xdata:XML = new XML(loader.data); // using E4x

    //xdata.child(0);

    for (var j:int=0; j<xdata.length(); j++) {
        onList[j] = xdata.child(j);

        for (var k:int=0; k<HouseObjects.length;k++) {
            //root[onList[j]].gotoAndStop(3);
            if (onList[j] == HouseObjects[k].name) {
                HouseObjects[k].gotoAndStop(3);
                //trace("tracing house objects"+ HouseObjects[k]);
                trace("onList[j]: " + onList[j]);
                trace("Array onList: " + onList);

            }

        }

    }

}



 } //end of class

} // end of package

You actually don't need to put all this code here, and it's true that you should make your app more secure , especially after showing all this info! If your XML is not the problem , check the Actionscript part, particularly the parseXML() function.

Are you able to trace the names of the components that are switched on? If yes , concentrate on what's happening in your loop. If your xml is fine, the problem is not passing data from PHP to Flash.

I like the tree house! ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM