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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
1 2 3 4 5 6 7 8 9 10 11 | <?php if($_POST[string]){ $upper = strtoupper($_POST[string]); echo "result=You just wrote $upper"; } else{ echo "result=You did not write anything"; } ?> |
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”
They can be easily customized to meet the unique requirements of your project.

























This post has 34 comments
Sam
Should be “You did not write anything”.
Thanks for the awesome tutorials Emanuele. Keep up the good stuff
Keiran
Great Job… but whenever you put ” in it comes up with /
Would there be a way to fix that?
Frederik J
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
Keiran
Frederik!
Thanks for letting me know… :-)
Xodus
Emanuele, nice job.
The only thing is that its not wrote…its write. its ok though.
Nathan
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!
Art
When you don’t send anything it sould be “You didn’t WRITE anything” not wrote. Great tut, like always =P
Emanuele Feronato
Sam,Xodus,Art: fixed
villas ibiza
Emanuele, nice one
Graham
Does this work in AS3?
Massimo M.
this is not case sensitive,
anyway very good work ! :)
Llama
are you going to make the MySql highscores tutorial?
these have been very helpful so far. :)
Keiran
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!
Llama
thanx, I didn’t see that…
Keiran
Your welcome Llama!
Tutorial | Sending, manipulating and receiving data with sendAndLoad / AS3 « Flash Enabled Blog
[...] 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 [...]
Roy
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)
Luis
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
ray
where should we put System.security.allowDomain(â€www.other.comâ€); ?
Alberto
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?
Kashyapa
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!
Sander
Best tut about sendandloadvar on the web! Thanks man!
Cyclone103
Thank you SO MUCH for writing this tutorial, it may have just made my game possible. I think I might have to bookmark this page…
I am working on an MMORPG, and now I can really get things started.
This tutorial is the most clear one I have seen about this on the internet.
Thanks!!!!!
Trang
When I use your php script from your server everything works fine. But for some reason when I use a script from my server I keep getting the server error. How can I fix this?
Sasa
Hello, thank you for the code, but I have a little problem with it. The code is working only when two files are on the same domain. As soon as they are apart flash allways outputs an error.. I’ve tried putting
System.security.allowDomain(â€www.other.comâ€);
but that wont work either..
I’m using CS3 AS2.. can enyone help? it’s an EMERGENCY :)
Sasa
I’ve tried couple of these scripts for hundreds thoousend of times and none of them as well as this one won’t work if swf is on one domain and php on another..
With this script, I tried copy-pasting it and adding allowDomain and still it is working only when testing in flash.. I’t wont work when I put it on server..
I’ve been working on this for two days now trying to make some kind of security for my work..
Does anyone know if this is even possible to work this way (with the files on separate domains..) and save me a lot of pain?
Sasa
Ok, i realized what is the problem… In order to load data with someflash.swf from one domain you have to have a otherflash.swf on the other domain that contains data…
I needed this thing for the following: I make a flash web site and give it to some company.. They delay their paymant or wont pay so I change my permisions data in permisions.txt on my domain and they can’t use the flash site untill they pay me.. a little bit of protection
If someone need’s scripts mail me at alesacar@yahoo.com or I can place them here if administrator would aprove it.
Thanks again and best of luck!
Geoff
Great tutorial! The working Flash example brings it all together… Well Done!
I had to change (POST to REQUEST) line 3 and 4 of PHP from:
if($_POST[string]){
$upper = strtoupper($_POST[string]);
to:
if($_REQUEST[string]){
$upper = strtoupper($_REQUEST[string]);
Zelalem
Hi, I wanted to use your example using cgi. But, I wanted to use it with cgi and failed to do so. I used param to recieve data from the flash doc. I used
$data=param(“post_variable”);
But I got “Undefined subroutine &main::param called ….”
I am sure it is a cgi error, but is the variable that i referred in perl (post_variable) right?
BTW, thank you for the woderful work.
George Greece
Best Tutorial MAn..
Great Job..
Thanks..
Moisés
Opa.. blz.. Baum demais este tutorial, tentei fazê-lo porém não obtive exito.
De forma alguma consigo um resultado do servidor… gostaria de saber se tem como eu baixar o seu fla, para estudar.
Ted
Hello!
I really like your tutorials, I think they’re very useful. I’d like to know, if there is a way to create online flash game on AS2, and will this function(sendAndLoad) be useful?
Suren
Great tutorial!
I need download the sources (.fla , .swf and . php)
Samad
Good tutorial,
Could this PHP script be called in a huge traffic context and in background?