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”

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (29 votes, average: 4.97 out of 5)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 34 comments

  1. Sam

    on June 1, 2008 at 1:46 pm

    Should be “You did not write anything”.

    Thanks for the awesome tutorials Emanuele. Keep up the good stuff

  2. Keiran

    on June 1, 2008 at 2:04 pm

    Great Job… but whenever you put ” in it comes up with /

    Would there be a way to fix that?

  3. Frederik J

    on June 1, 2008 at 3:06 pm

    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

  4. Keiran

    on June 1, 2008 at 4:17 pm

    Frederik!

    Thanks for letting me know… :-)

  5. Xodus

    on June 1, 2008 at 4:24 pm

    Emanuele, nice job.
    The only thing is that its not wrote…its write. its ok though.

  6. Nathan

    on June 1, 2008 at 5:18 pm

    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!

  7. Art

    on June 1, 2008 at 6:00 pm

    When you don’t send anything it sould be “You didn’t WRITE anything” not wrote. Great tut, like always =P

  8. Emanuele Feronato

    on June 1, 2008 at 10:11 pm

    Sam,Xodus,Art: fixed

  9. villas ibiza

    on June 1, 2008 at 10:39 pm

    Emanuele, nice one

  10. Graham

    on June 1, 2008 at 10:53 pm

    Does this work in AS3?

  11. Massimo M.

    on June 2, 2008 at 2:53 am

    this is not case sensitive,
    anyway very good work ! :)

  12. Llama

    on June 2, 2008 at 4:07 am

    are you going to make the MySql highscores tutorial?
    these have been very helpful so far. :)

  13. Keiran

    on June 2, 2008 at 10:28 am

    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!

  14. Llama

    on June 2, 2008 at 10:31 pm

    thanx, I didn’t see that…

  15. Keiran

    on June 3, 2008 at 4:24 pm

    Your welcome Llama!

  16. Tutorial | Sending, manipulating and receiving data with sendAndLoad / AS3 « Flash Enabled Blog

    on June 5, 2008 at 1:09 am

    [...] 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 [...]

  17. Roy

    on June 12, 2008 at 10:28 pm

    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)

  18. Luis

    on June 14, 2008 at 5:08 am

    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

  19. ray

    on June 30, 2008 at 2:04 pm

    where should we put System.security.allowDomain(”www.other.com”); ?

  20. Alberto

    on July 21, 2008 at 1:57 am

    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?

  21. Kashyapa

    on July 28, 2008 at 12:38 pm

    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!

  22. Sander

    on October 2, 2008 at 1:15 pm

    Best tut about sendandloadvar on the web! Thanks man!

  23. Cyclone103

    on January 26, 2009 at 9:12 pm

    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!!!!!

  24. Trang

    on March 18, 2009 at 1:13 am

    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?

  25. Sasa

    on March 25, 2009 at 10:10 pm

    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 :)

  26. Sasa

    on March 25, 2009 at 11:18 pm

    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?

  27. Sasa

    on March 27, 2009 at 5:41 am

    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!

  28. Geoff

    on May 8, 2009 at 3:41 pm

    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]);

  29. Zelalem

    on May 15, 2009 at 4:08 pm

    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.

  30. George Greece

    on July 31, 2009 at 4:36 pm

    Best Tutorial MAn..
    Great Job..
    Thanks..

  31. Moisés

    on August 15, 2009 at 9:15 am

    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.

  32. Ted

    on June 18, 2010 at 2:35 pm

    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?

  33. Suren

    on December 1, 2010 at 11:28 am

    Great tutorial!
    I need download the sources (.fla , .swf and . php)

  34. Samad

    on April 19, 2011 at 6:05 am

    Good tutorial,
    Could this PHP script be called in a huge traffic context and in background?