Top 3 Products & Services
1. 2. 3. |
Dated: Aug. 12, 2004
Related Categories
FlashWritten By Rent-A-Tutor
To load and save text in Flash is not a trivial task. There are some pitfalls that should be considered before using LoadVariable() to read and write data.
Loading Text
Loading a text in Flash is quite easy as long as you consider some technical problems according to the browser cache.
You can simply place the following line in a Keyframe of your flash movie:
loadVariablesNum ("./data.txt", 0);
whereas "data.txt" is a text file on your web server. The text should have the following format:
variableName=variableValue&var2=value2&var3=value3
The same format would be used to pass variables within a URL line.
If you examine this in the browser, it might work for the first time. If you will change the content of "data.txt" on the web server, some browsers do not read the correct values in the flash movie, because they will store the content of "data.txt" in the browser cache.
The reason for this behavior is: The command loadVariableNum() is the same as GetURL() which will be cached by default.
To fool the browser to reload the text file every time, you can pass a dummy variable after the filename with a value that will change after each call:
timer = new Date();
loadVariablesNum ("./data.txt?time="+timer.getTime(), 0);
The time variable will contain the current time from the client's computer and the method getTime() will retrieve this time in seconds. So the url will have an additional parameter "time" that will change every time you call the function.
Saving Text
To save text on the web server, you need a server side scripting language like perl, php or ASP. I have chosen PHP for this example because it is easy to place a php script in the same directory as the flash movie.
Unfortunately, there are some problems if you use the built-in GET and POST option with LoadVariableNum(). For some reason, sending Variables by POST only works in Internet Explorer and not with Netscape and Opera. For this reason, the variables will be passed directly in the URL string.
This is more convenient than sending them all with the GET option, because you need to pass only the values that you want to store.
time = new Date();
url = "./store.php?time="+time.getTime()+"&varName="+escape(varName);
loadVariablesNum (url, 0);
In this example the variable "varName" will be passed. Make sure to use identical names on the left and the right side of the equal sign. The URL that you call is "store.php" that is located in the current directory.
The function escape() will convert special characters, for example blanks, so that they will not be misinterpreted as additional parameters.
Calling store.php may produce the same problems with browser cache, even if you pass different values. Just imaging that another user is browsing your page and has changed the value and you enter a value which you have already passed. The browser cache would recognize the call with the same parameters and would not bother to call the script on the server because it can be processed in the local cache. In this case, the data would not be updated on the server.
The content of the script looks like this:
$fp = fopen("data.txt", "w");
if (!$fp) die ("cannot open the file");
fputs($fp, "&varName=".$varName, 4096);
fclose($fp);
?>
The fopen() command just opens a text file "data.txt" in write mode "w". If the result is not a valid file handle, the script will die with an error message. You will never see the error message, because you call the script with LoadVariables() which will not show a new browser window for the URL, but it may come in handy if you want to call the script manually for testing.
The command fputs() will take the file handle $fp as the first parameter. The second parameter is the text to write. In this case, the variable name and the value are written in the necessary form "&varName=value".
The third parameter is the maximum length that will be written. In the example, 4096 is chosen, but you should keep in mind that the URL may have some limitation in length.
After all variables are written (you can write as many variables as you like), the file is closed with fclose().
The Example
(example: download here)
The example contains a flash movie and a php script. The flash movie contains two text fields and a button. The actionScript code for the first Keyframe can be seen in the next image:
Image1: The first and only Keyframe of the flash movie with actionScript code.
The button contains the code for passing the two text fields to the php script:
Image2: The actionScript code for the button.
The script store.php contains all the necessary code that is required to save the variables:
// store the variables passed by POST
$fp = fopen("data.txt", "w");
if (!$fp) die ("cannot open the file");
fputs($fp, "&myMail=".$myMail, 4096);
fputs($fp, "&myText=".$myText, 4096);
fclose($fp);
?>
To use the example on your website, upload message.html, message.swf, data.txt and save.php to your web server. All files must be stored in the same directory. The file data.txt must be writeable by all users, so you should execute a chmod 606 (or chmod 666 depending on your host) with your FTP-Client on that file.
Now load the message.html with any web browser and enter some data into the fields "your e-mail" and "your message". Click on "send" to store the value. If you come back to the same page or load it into another browser window, the stored text should appear in the fields.
Conclusion
This method is convenient to pass small text values or numbers from a flash movie to the web server and back again.
Now that you've gotten free know-how on this topic, try to grow your skills even faster with online video training. Then finally, put these skills to the test and make a name for yourself by offering these skills to others by becoming a freelancer. There are literally 2000+ new projects that are posted every single freakin' day, no lie!
![]() Previous Article | ![]() Next Article |
I see your point Ingrid. I agree. We also use Windows IIS for dev. purpose, before launching the app on our Linux/Apache setup. In all honesty, if all variables remain the same; Linux will outperform Windows and Apache will outperform IIS.
And this is coming from someone who's a Windows User since 1997.
![]() |