Flash sending, manipulating and receiving data with sendAndLoad
This is a very importat thing to know when you are about to design Flash forms, or any application that needs to manage data such as an highscore table.
This is a standalone tutorial, but the basics explained will be useful in the Creation of a Flash highscores API course.
The only method we need to know is sendAndLoad
Let's dive into an example: I created an input text area called input_text, a button called send_button and a dynamic text called result_text.
Now, look at the code:
-
var php_process:LoadVars = new LoadVars();
-
send_button.onRelease = function() {
-
var post_variable:LoadVars = new LoadVars();
-
post_variable.string = input_text.text;
-
post_variable.sendAndLoad("http://www.emanueleferonato.com/downloads/sendtest.php",php_process,"POST");
-
};
-
php_process.onLoad = function(success:Boolean) {
-
if (success) {
-
result_text.text = php_process.result;
-
}
-
else {
-
result_text.text = "Error connecting to server.";
-
}
-
};
Line 1: Declaring a variable called php_process as LoadVars
The LoadVars class is useful for transferring variables between a Flash application and a server (who said "like an highscore"?)
Line 2: Beginning of the function to be executed when the send_button button has been pressed
Line 3: Declaring another LoadVars variable called post_variable
Line 4: Setting the string attribute of the post_variable as the content of the input_text dynamic text area
Line 5: Using sendAndLoad to pass post_variable to a script on the server at the address http://www.emanueleferonato.com/downloads/sendtest.php using the POST method and saving the result in the php_process variable
Line 7: Beginning of the function to be executed when the php_process has been loaded (when the server replied). Notice the success boolean flag
Lines 8-10: If success is true (I communicated with the server), then display in the result_test dynamic text area the result variable of php_process. We'll see how to get the result variable when we'll examine the php script
Lines 11-13: Dispaying an error if I wasn't able to communicate with the server
Now, let's see the php script in the
http://www.emanueleferonato.com/downloads/sendtest.php page
Line 3: If the string variable passed with POST method contains something...
Line 4: Convert the string to uppercase, just to do something...
Line 5: writing the content of the result variable as the uppercased value of the initial string
Lines 7-9: if the string variable passed with POST does not contain anything, then writing the content of the result variable as a message saying you did not write anything.
And that's it... play with it and give me feedback
Write something in the upper text area and press "send"
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
21 Responses to “Flash sending, manipulating and receiving data with sendAndLoad”
Leave a Reply

Should be “You did not write anything”.
Thanks for the awesome tutorials Emanuele. Keep up the good stuff
Great Job… but whenever you put ” in it comes up with /
Would there be a way to fix that?
Hi Emanuele.
Nice job. I like when you mix PHP and Flash, but I also want to see something more advanced :-)
@Keiran:
Yes of course you can do that. Why it does it is because of something called magic quotes. Which helps against mySql injection.
To remove them you simply just add a line of php. Add the following line between line 4 and 5:
$upper = stripslashes($upper);
/Frederik J
Frederik!
Thanks for letting me know… :-)
Emanuele, nice job.
The only thing is that its not wrote…its write. its ok though.
It took me forever to figure this out:
If you have AS2 (i’m not sure if it works for AS3)
it should be:
“post_variable.String = input_text.text;”
the S on string is uppercase
anyway awesome tutorial!
When you don’t send anything it sould be “You didn’t WRITE anything” not wrote. Great tut, like always =P
Sam,Xodus,Art: fixed
Emanuele, nice one
Does this work in AS3?
this is not case sensitive,
anyway very good work ! :)
are you going to make the MySql highscores tutorial?
these have been very helpful so far. :)
Llama,
There are already some highscore tutorials up for example “100 ROUNDS”
Just google that or look for it in the website…it is a very helpful tutorial!
thanx, I didn’t see that…
Your welcome Llama!
[...] Read Tutorial No Comments Leave a Commenttrackback addressThere was an error with your comment, please try again. name (required)email (will not be published) (required)url [...]
Hi Emanuele, can i ask you something?
I need to use a sendAndLoad from a swf from a specific server (wwww.some.com) but this server cant recive php files… I put the php files into another server (www.other.com) and the swf(sendAndLoad) with “www.other.com/file.php”…
that it works??…
becose… dont work!! jaja.
do you know why??
well, good luck and excuse my inglish. (Im from Argentina)
Roy, I think you will need to specify the
System.security.allowDomain(”www.other.com”);
In order for the flash swf to communicate with the php in the other server.
———-
Es decir, necesitas en el fla poner el
System.security.allowDomain(”www.other.com”);
Para que el swf pueda comunicarse con el php.
Saludos desde Mexico
where should we put System.security.allowDomain(”www.other.com”); ?
Bravisimo! Good explanation, simple application. Good work.
Can you help me with this? I need the visitors of a website draw a personal mark, save it and send it to a site elsewhere to collec all the drawings. How can I do that?
Dear Emanuele,
Thank you very much for publishing this.
It was really helpful for me where we(with PHP guy too) were breaking our heads on small thing,and this only pointed out the basics.Thank you again!