Sending Variables to Server
 

Sending Variables to a Server

Auto URL encoded, so no worries, just need correct AS and know which vars flash sends.

Using loadVariables to send vars
Responsible for sending as well as recieving vars. All you do to send vars to a PHP, CGI or ASP script is specify GET or POST at the end of the loadVariables action. In the URL field just give the file location(path) of the script your sending to.

loadVariablesNum ("http://www.mydomain.com/mysript.php", 0, "GET");

But which vars is Flash sending? All the vars in the timeline that executes the script, if on main timeline - all the vars on there, including odd ones floating about you didn't intend to send. If an instance in a Mc, all the vars declared for that instance. So it's necessary to put Mc instances in the movie just to hold vars, then you can put the loadVariables action in that Mc, or better, use OO to let the command come from anywhere in flash.

_root.ball.loadVariablesNum
("http://www.mydomain.com/mysript.php", 0, "GET");
s

This can be executed from anywhere in flash, it'll send the vars of 'ball' to the Url address. Return vars are sent to level0. this gives powerful flexibility.

Watch out for objects and functions. flash also sends objects and functions with the vars. getURL is good way to see what types of things Flash is sending out. The test (p43) shows how flash sends all the stuff in the URL field, not instances of MC's though. Therefore it demonstrates the need to create an Mc to store variables.

Plan ahead to see where to place important vars you'll be sending to the sever. then just add the GET or POST method on the end of loadVariablesNum to force flash to start sending the goods. this is the way to send e-mail, form data to a CGI/Perl script, or info on a product order to PHP and mySQL. You'll' be using this a lot during all the tutes.

loadMovie and 'GET'. If you need to send vars to another flash movie you're loading via loadMovie or loadMovieNum just specify GET at the end of the statement. Vars immediately load on main timeline ready for action. Works even when replacing your level0 movie if you need to work with multiple framerate flash movies, but still retain your vars. POST doesn't work in this case.

Sending Arrays to Servers. Like all the other stuff flash sends - in Url encoded in a string with comma seperation of the elements -- see how it does it with getURL. code on button:
authors="Mark and Bill";
carColors = new Array("red", "white", "blue", "green");
getUrl("http://www.netclub22.com/nofile.html", "_blank", "GET");


Press the button - see the info in the URL address. %2C is the code for a comma. Need to account for this on the server side to bereak up the string to use as you want. If just storing info in atext file - no need , flash converts back from string with 'split'.

Alternate ways to Load and Send Variables.
HTML for display in flash. In a string variable, limited html code. Switch on HTML format in the testfield properties.Don't include <head>.code on button: loadVariablesNum ("htmlcode.txt", 0);

in HTML.txt:
mycode=<html>
<body bgcolor="#000000" text="#FF6699">
<font color="#FF0066">Two website links:</font><br>
<a href="http://www.sandlight.com"><font color="#FF6666">Bill Sanders</font></a><a href="http://www.flashcore.com"><br>
<font color="#0066FF">Mark Winstanley</font></a>
</body>
</html>

External ActionScript.
.as files. Via #include - to load into falsh. Same security, only HTTP addresses on same server of website. In whatever frame(or clip action) you want the actions to appear in - just put an include in:
#include"filename.as" No semi-colon.

You can load in variables or whatever Actionscript you like - drawback? - You won't get experience using the Internet standard of URL encoding to operate on variables.
caution: #include files seem to behave differently to ordinary textfiles using loadVariables. - don't always appear in browser catche. For consistancy - use dynamic query string method, or server-side scripting.

HTML and Javascript Vars loading. Using JS or direct HTML you can pass vars directly to flash from the HTML page where plug in is embedded. Works in all browsers, but not sending vars back to the page sometimes.

Easy to define vars right in the html page. You may wonder how to place vars in flash directly on generating the page. It's simple. Add a query string onto the filename following the swf extension, inside the <object> and <embed> tags.

<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
WIDTH="400" HEIGHT="300" id="madDuck" ALIGN="">
<PARAM NAME=movie VALUE="circus.swf?clown1=Happy&clown2=Honker"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF>
<EMBED src="circus.swf?clown1=Happy&clown2=Honker" quality=high bgcolor=#FFFFFF WIDTH="400" HEIGHT="300" NAME="madDuck" ALIGN=""
TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>

all I did here is add this Query string to the end of the embedded swf (as above):
?clown1=Happy&clown2=Pissed Off. The variables are passed directly to the swf - available on the main timeline - and displayed in the text fields as you see.

Only one chance to pass vars - when the swf is first embedded. Only loadVars from then on or JS as described next. Security - all vars visible in the query string.

Forcing vars into Flash with Javascript. Flash is just another object to JS, Give unique name to the flash movie, JS can reference it via document.object.SetVariable syntax. Can set vars after movie has loaded. Name the swf lilke above in red, in quotes. It's just a reference name, nothing to do with Param, and the name can be different to the swf filename (I've use "madDuck" just for illustration).

<SCRIPT LANGUAGE=javascript>
document.madDuck.SetVariable("clown1", "Ecstatic");
document.madDuck.SetVariable("clown2", "Manic Depressive")'
</SCRIPT>


Before JS Reference:
Nada!(No name designated for JS to reference)

After giving the swf a name - and the JS code in this page which is inserted just after the Flash plug in code.
RESULT!
The code is right in here - just after the swf Object/embed.

You can do this:
var nameOfClown="Happy";
document.madDuck.SetVariable("clown1", nameOfClown);


Using JS can come in very handy. You can have HTML form values sent inoto flash. Or set a function in the Head tag and call in the body with onLoad tag and continually pass data to flash. Yep - you've got a pipeline straight into Flash through JS and continually feed new and updated info by using JS SetVarable. Need to coordinate elements in the flash movie to do something with the data that's arriving. Again - Security, everything is visible. But for simple things and fun movies it's a great tool.

Callin JS from Flash.
Flash can call any JS function defined in your html page. Not cross-platform, especially Mac. It only involves the getURL action, instead of the URL you enter JS. If you had a date function defined (which I have, he he), you make a button and stick this on it: getURL("javascript:getDate();") and it opens up a whole new browser window with the date in. (I got carried away here and went and did the whole newWindow() thing and called that instead with the date function on the page I called -so it looks good!). So the code I've put on the button is actually:
getURL("javascript:newWindow();") here's the getDate code, just for this html page: getDate(); giving us this:



and here's the Flash Button:


As well as calling pre-defined functions, you can call just about any JavaScript code.Say you needed a button to set a JS var in the html page named clown1 to a value of Happy. button code:
on(release){
    getURL ("javascript:var clown1=\"Happy\";");
}


Secret Fash Projector AS. Projector file now has ability to send vars straight to the hard drive. Via FSCommand. 'save' Creates text file on users hard drive. Great for stand alone flash games. Ex:

Create secretFS.fla - vars on the 1st Frame and this on a button:

on(release){
fscommand("save", "fs_data.txt");
}

and .. it doesn't work - and I don't care, hmm quick reference indicates macromedia have removed this feature for the current standalone Player.- onwards (P.55) .

 

back to index   next