Wednesday, September 18, 2024

Exploring loadVars.sendAndLoad, write to a text file and get the data back into flash

Hello people lets take a look at loadVars.sendAndLoad and what we could do with it today, to read more about it go here:

http://www.macromedia.com/support/flash/action_scripts/ actionscript_dictionary/actionscript_dictionary427.html

We are going to send data from an input textfield in flash to a php script with sendAndLoad. The php script will place that data in a textfile. The data is also immediately sent back to flash so that we can output it there. Also some error checks will be outputted to flash to show us the status of writing to the file. You can press as often as you want on the submit file and what’s in the input text box will be send to the text file. I havent put in any checks if the textfield maybe is emty or something so you could get empty rows in the textfile. This file is just to show all of you interested how you could use sendAndLoad to send and retieve data. So its just to play around with and i hope you will learn something from it.

Pre setup:

Go to your web folder and make a folder named “exercise”. If you havent setup a localhost yet you could tryout my getting php data to flash tutorial. In that tutorial i explained how to setup testing environment for php via a little gem named “phpdev”( sets up apache server, php, phpmyadmin… i love it).
http://www.actionscripts.co.uk/tutorials/erwin/03/index.html

So if you followed that tut, your webroot is c:\phpdev\www\. so you make the “exercise” folder there and make sure you’re apache server is running. So now you should have something like:

c:\phpdev\www\exercise\

You can browse to that folder by opening up your favorite browser and type as url:

http://localhost/ and your apache environment should show up. now just browse to the folder you made and you are set.

– Now Openup notepad or some text editor and make a new file.

– Save this empty text file as “myTextFile.txt” in the exercise folder( c:\phpdev\www\exercise)

Step 1: Setting up the php part

– Openup your favorite editor (i used sciTE|FLASH)

– Make a new file and save it as careTaker.php in the exercise folder.

– Now place this code in the php file.

[PHP open TAGS HERE]//phps opening tags are " @file("membersDatabase.txt");
&nbsp&nbsp //we use the $count variable in the string we send to the text file,it attaches a number to the string for us
&nbsp&nbsp //to show how may times we inserted someting into the textfile.
&nbsp&nbsp foreach($txtfileArray as $count => $member);

&nbsp&nbsp //We go to the last byte of the file, because we want to insert data at the end of the file
&nbsp&nbsp $gotoLastByteOfTxTFile = @fseek($myTextFileHandler,0,SEEK_END);

&nbsp&nbsp //Increase count by one because an array starts counting at 0.
&nbsp&nbsp $count = $count + 1;

&nbsp&nbsp //Here we make a variable writeInTxtFile with the command @fwrite.
&nbsp&nbsp //It will write a string into the text file defined in myTextFileHandler, notice the \n makes it start on a new line each time .
&nbsp&nbsp $writeInTxtFile = @fwrite($myTextFileHandler,"\nreceived data from flash $count = $receivedFromFlashData");
&nbsp&nbsp //Was writing into the txtfile a success , if true print a variable to flash to be handled in the response loadVars Object.
&nbsp&nbsp if($writeInTxtFile){

&nbsp&nbsp&nbsp //Here we make a variable to hold the message we wanna show in flash if writing to the textfile was a succes.
&nbsp&nbsp&nbsp $writeStatus = "writing to textfile was a succes";

&nbsp&nbsp&nbsp //This is printed to flash and this piece of data is actually attached to the loadvars object we defined in flash.
&nbsp&nbsp&nbsp //So you can acces it just like you would a variable in a movieclip or object.
&nbsp&nbsp&nbsp //so you can set this variable to be show in a textbox like this:
&nbsp&nbsp&nbsp // mytextbox.text = myReveivingLoadvars.writeStatus
&nbsp&nbsp&nbsp // this piece of code must be triggered in the myReveivingLoadvars.onLoad part
&nbsp&nbsp&nbsp print("&writeStatus=$writeStatus");

&nbsp&nbsp //if writing was a failure we print that to flash
&nbsp&nbsp }else{
&nbsp&nbsp&nbsp $writeStatus = "writing to textfile was a big failure";
&nbsp&nbsp&nbsp print("&writeStatus=$writeStatus");
&nbsp&nbsp };

&nbsp&nbsp //here we close the stream to the textfile
&nbsp&nbsp @fclose($myTextFileHandler);

&nbsp&nbsp //If opening the text file was a failure in the first place we print this to the php output.
&nbsp&nbsp //You could choose to also send error messages like this to flash, just change the print command to something like:
&nbsp&nbsp //print("&failMsg = $youErrorMsg"); and call the failMsg variable in your flash file in the receiving onLoad part of
&nbsp&nbsp //the loadVars object e.g. like myTextBox.text = myReceiveLV.failMsg.
&nbsp&nbsp }else{
&nbsp&nbsp&nbsp print("opening txtfile has failed\n");
};

//this is printed to flash and received by the reveiver part of our loadVars object.
//this piece of data will be show in a textbox in flash immediately after a you press submit.
// so you sumbit your inputted text in flash , its ran through this little engine like php script and you immediately receive
// an answer back.
print("&receivedData=$receivedFromFlashData");

[PHP closingTAGS HERE] //"?" + ">"

Step 2: Setting up the flash file

– make a new flash file: 300 x 300 with a dark grey background

– Make an input textbox named “inputData”

– Place a static textfield next to it with this text (or something like it) “input some text to store:”

– Draw a little square and convert it into a movie clip (select it and press f8) this will become the submit button.

– In the convert to symbol menu set these values:

&nbsp&nbsp * name to “submit”

&nbsp&nbsp * behavior to movieclip

&nbsp&nbsp * registration point in the middle

&nbsp&nbsp * export for actionscript must be selected.

– Select the clip again and set its instance name also to “submit”

– Make a dynamic textbox to display the status of writing to the textfile.

– Give that dynamic textbox an instance name: “statustxtb” and set its dimensions to about 270 x 17.

– Make another dynamic textbox, instance name “responsetxtb” and about the same dimension as the input textbox.

– This last textbox will hold the response we get from the php file and displays the data we inputted in the input field.

– You could choose to set some describing static textfields next to the dynamic text field, but that a personal choice

Step 3: Setting up the actionscript part

– Make a new layer in your fla and name it code place this code on the timeline.

//First we write a function to... well the name says it all.
function submitDataToTextfile() {
&nbsp&nbsp //this is our sending loadVars object, it sends the data to the php file so that it can be processed.
&nbsp&nbsp submittedData = new LoadVars();

&nbsp&nbsp //here we make a variable "inputData" and as value we set it to the value of the inputData textfield.
&nbsp&nbsp submittedData.inputData = inputData.text;

&nbsp&nbsp //this is our response loadVars object, it is responsible for receiving data from the php script immediately
&nbsp&nbsp //when submit is pressed it will receive its data
&nbsp&nbsp response = new LoadVars();

&nbsp&nbsp // this is an important part to understand, we execute the function "doThisOnResponse" in the response.onLoad.
&nbsp&nbsp // so when the onLoad is triggered this function runs and the data is handled by flash how you set it up.
&nbsp&nbsp response.onLoad = doThisOnResponse;

&nbsp&nbsp //finally sendAndLoad to the php script, we need to post the data to our php script.
&nbsp&nbsp //IMPORTANT , make sure you set the right path to the php file or else it wont work
&nbsp&nbsp submittedData.sendAndLoad("http://localhost/exercise/careTaker.php", response, "post");
};

//this is the function that runs on response, this function makes sure everything is placed in the right places.
function doThisOnResponse(result) {
//if the result is true, so if everything went ok and the data has been received, do this:
&nbsp&nbsp if(result){
&nbsp&nbsp&nbsp // place the value from "response.receivedData" into our textbox "responsetxtb"
&nbsp&nbsp&nbsp //notice how you just call a variable that was atatched to our response loadVars object.
&nbsp&nbsp&nbsp responsetxtb.text = response.receivedData;

&nbsp&nbsp&nbsp // we also made a status check in the php file, this is where the value from that variable comes in and
&nbsp&nbsp&nbsp // will be placed in the "statustxtb" textbox.
&nbsp&nbsp&nbsp statustxtb.text = response.writeStatus;
&nbsp&nbsp }else { //else there went something wrong.
&nbsp&nbsp&nbsp trace("everything went wrong...aaghhh..damn this");
&nbsp&nbsp };
};

// if we press submit submitDataToTextfile() function is executed an the engine is started.
submit.onRelease = function() {
&nbsp&nbsp submitDataToTextfile();
};

Well this was my tutorials about loadVars.sendAndLoad in combination with some txt file manipulation, i hope you enjoyed it.

My name is Erwin Schiphouwer a.k.a deamothul, im 25 and live in Groningen in
the Netherlands.
Im totally hooked on actionscript and especially in combination with php and
mysql.
I taught what i know by myself and still learn new things every day.
I hope to spread some love for scripting and make it a bit more easy for
people just starting out.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles