Flash sendandload – XML


I am using Flash 8. this is the only thing you need if you need to follow this example 

 On the flash IDE – stage need to have a Button Component (button name – submit_button)  and Dynamic text with (result_ta)
If you want to send xml data from flash to your dynamic page,

you know the syntax – send("url","POST/GET")

if you want to load an XML data from a url you still know the syntax load("URL"); 

well how about sending an XML data to a server page and receiving the same as XML. Yes you are right it is sendAndLoad("URL", receiving Object, "GET/POST");

All of the above you can find in flash HELP, but there is a tricky part which has been left out.  

Let me place the Actionscript first and then the PHP serverside script and explain in detail.

ActionScript:

var submitListener:Object = new Object();
submitListener.click = function(evt:Object) {
    var result_lv:LoadVars = new LoadVars();
    result_lv.onLoad = function(success:Boolean) {
        if (success) {
            result_ta.text = result_lv.welcomeMessage;

        } else {
            result_ta.text = "Error connecting to server.";
        }
    };

    var result_xml:XML = new XML();
    result_xml.ignoreWhite = true;
    result_xml.onLoad = function(s){
        if(s){
            result_ta.text = this.firstChild;
        }else{
            result_ta.text = "Error connecting to server.";
        }
    }

    var send_lv:LoadVars = new LoadVars();
    var xm:XML = new XML("<root><name>vinod</name></root>");
    xm.ignoreWhite = true;
    xm.contentType = "text/xml";
    send_lv.xm = xm;
    send_lv.sendAndLoad("http://vinodvv/test/sendandloadxml.php&quot;, result_lv, "GET");
    send_lv.sendAndLoad("http://vinodvv/test/sendandloadxml.php&quot;, result_xml, "GET");
};
submit_button.addEventListener("click", submitListener);

PHP Script:

 <?PHP
$xml = $_GET['xm'];
echo $xml;
?>

Ok let us discuss the flash help, type the keyword "sendAndLoad" and select the same in your flash IDE and press F1, well you will read the above pasted Actionscript with few lines of code which I have added extra. 

point 1 to remember if your server page doesn't recognise the data what you are sending

xm.contentType = "text/xml";

This is the reason, the data what you send is of string type you need to specify as xml type.

Point 2 to remember server side page just responds to your command and sends you the XML data, but the XML data on the flash side becomes irrelevant as it needs a parameter to accept data as string in the flash example. 

result_lv.welcomeMessage; 

 welcomeMessage=XML should be the way you serverpage need to write data, but this is absurd, and this is nothing but string type and not XML, you will have create a new XML object to embrace this string to an XML typ, so Avoid this and try this one.

I am using the same way you load the normal XML data from a URL, all you have to do is define a new XML variable and declare most of the commands

var result_xml:XML = new XML();
    result_xml.ignoreWhite = true;
    result_xml.onLoad = function(s){
        if(s){
            result_ta.text = this.firstChild;
        }else{
            result_ta.text = "Error connecting to server.";
        }
    }

and don't specify the command result_xml.load("url") instead specify it over here

send_lv.sendAndLoad("http://vinodvv/test/sendandloadxml.php&quot;,

This should do the job. try it yourself, if you find any other better approach do make a comment. 

Advertisements

8 thoughts on “Flash sendandload – XML

  1. Hello, can you tell me if you can use this on a frameset.

    I want to send this data to another frame in my frameset. How can I do this? since you can’t tell where to send this data.

    Or do I understand wrong?

  2. Ok, the script work out, but, if i add an XML.LOAD(“URL”) to “populate” the xml to be send it just wont work. All the data send is messed up.

    var result_xml:XML = new XML();
    result_xml.ignoreWhite = true;
    result_xml.onLoad = function(s) {
    if (s) {
    result_ta.text = this.firstChild.childNodes[0];
    } else {
    result_ta.text = “Error connecting to server.”;
    }
    };
    var send_lv:LoadVars = new LoadVars();
    var xm:XML = new XML();
    xm.load(“http://localhost/spam.htm”);
    xm.ignoreWhite = true;
    xm.onLoad = function() {
    var xm_tmp:String = escape(xm.toString());
    send_lv.xm = xm_tmp;
    //trace(send_lv);
    send_lv.sendAndLoad(“http://localhost/spam3.htm”, result_xml, “POST”);
    };

    This is the way i modified the script, pretty much i just added an xm.onload, i dont know why it wont work, unless i use the escape command; when i trace the variable after using the onload, is all messed up. Any comments, im pretty much a noob with AS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s