TechiWarehouse.Com


Top 3 Products & Services

1.
2.
3.

Dated: Aug. 12, 2004

Related Categories

Flash

Written 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


Basbous's Comment
The honesty of your potsnig shines through
10 Thu Jan 2013
Admin's Reply:

Thanks Basbous. I really do appreciate all the nice things the visitors tell me here.




java's Comment
java ia a java
11 Wed Jul 2012
Admin's Reply:

Wow, what a revelation




Ingrid's Comment
I seriously doubt that. If you get stuck and saerch an answer on the web, most of the people willing to help you will assume you're using Linux. And what about load balancing and caching? What will you use? Does memcached work on Windows?And why? What major advantage does Windows bring me? I've coded some apps in PHP that don't work on windows. They don't even include Linux in their description. See the link, they say that now PHP runs faster on windows that it used to. They don't say that it will run faster on Windows than it would on Linux.It's so lame I don't even take it into consideration
15 Thu Mar 2012
Admin's Reply:

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.




vijith's Comment
Windows XP PRO has functionality for runinng a web server built inIIS is an option, however, under a "Default" installation of XP Pro, it might not be installedControl panel > add / remove programs -> Add Remove Windows COmponents Check "Internet Information Services"You could also look @ apache as has been mentioned, however, IIS is PART of windows.
15 Thu Mar 2012
Admin's Reply:

Excellent description vijith. Short, factual and to the point.




Manuel's Comment
Woaouuu . you're really wrong on C tmtaer. It is the first one language you have to learn in order to work as a programmer in any field. Because it is the language which will teach you good programming, memory usage efficicency, the real way the computer is using its parts to do what you want it to do, CPU efficient usage, disks efficient usage, bandwidth efficient usage. Almost ALL other languages as Python I work with every day (and what a good langauage !!!) or java are built with C. Learn C, and welcome to the real computers world. After that you will be able to learn whatever language you want. It will be easy. You will be able go from one to the other easily. I began in the industry with C on Hp-UX, YES !!!! building transactionnal apps. Programming is so easy now.
12 Mon Mar 2012
Admin's Reply:

To a great extent, I would agree with Manuel. C may not be object oriented, but it sure is a great mother of many programming languages out there. And almost all great programmers have had to give lots of time to understand C.




Josephine's Comment
you're pretty much oneiatrpg system independent check out Umbrello in linux. It lets you put together UML diagrams that it'll translate into a Java or C++ project (maybe others). It doesn't let you visually create any implementation really but it is good for high level planning and can make it easier to see how each component will be involved. I've been thinking about building something in processing that lets you see different levels of detail on an object when you get a certain distance away from it I think this would be a good approach to the kind of UI you're talking about, but I haven't built it yet so having it generate code is out of the question for now :). Anyway don't have time to keep writing here, but thanks for the read.
12 Mon Mar 2012
Admin's Reply:



Hasan's Comment
Thank you, your example hpeled me a lot. I couldn't figure out the relation of FileAppend and Gui, Submit. Again, thank you very much. Also, more than just some HTML is OK lol jk.
12 Mon Mar 2012
Admin's Reply:

 Thank you hasan :)




Eu mesmo's Comment
QUE MERDA DE ARQUIVO E ARTIGO
25 Thu Feb 2010
Admin's Reply:

Desculpe, meu Português é bom.






Facebook