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:

ACTIONSCRIPT:
  1. var php_process:LoadVars = new LoadVars();
  2. send_button.onRelease = function() {
  3.     var post_variable:LoadVars = new LoadVars();
  4.     post_variable.string = input_text.text;
  5.     post_variable.sendAndLoad("http://www.emanueleferonato.com/downloads/sendtest.php",php_process,"POST");
  6. };
  7. php_process.onLoad = function(success:Boolean) {
  8.     if (success) {
  9.         result_text.text = php_process.result;
  10.     }
  11.     else {
  12.         result_text.text = "Error connecting to server.";
  13.     }
  14. };

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

PHP:
  1. <?php
  2.  
  3. if($_POST[string]){
  4.     $upper = strtoupper($_POST[string]);
  5.     echo "result=You just wrote $upper";
  6. }
  7. else{
  8.     echo "result=You did not write anything";   
  9. }
  10.  
  11. ?>

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"

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5 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.

22 Responses to “Flash sending, manipulating and receiving data with sendAndLoad”

  1. Sam on June 1st, 2008 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 1st, 2008 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 1st, 2008 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 1st, 2008 4:17 pm

    Frederik!

    Thanks for letting me know… :-)

  5. Xodus on June 1st, 2008 4:24 pm

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

  6. Nathan on June 1st, 2008 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 1st, 2008 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 1st, 2008 10:11 pm

    Sam,Xodus,Art: fixed

  9. villas ibiza on June 1st, 2008 10:39 pm

    Emanuele, nice one

  10. Graham on June 1st, 2008 10:53 pm

    Does this work in AS3?

  11. Massimo M. on June 2nd, 2008 2:53 am

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

  12. Llama on June 2nd, 2008 4:07 am

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

  13. Keiran on June 2nd, 2008 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 2nd, 2008 10:31 pm

    thanx, I didn’t see that…

  15. Keiran on June 3rd, 2008 4:24 pm

    Your welcome Llama!

  16. Roy on June 12th, 2008 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)

  17. Luis on June 14th, 2008 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

  18. ray on June 30th, 2008 2:04 pm

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

  19. Alberto on July 21st, 2008 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?

  20. Kashyapa on July 28th, 2008 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!

  21. Sander on October 2nd, 2008 1:15 pm

    Best tut about sendandloadvar on the web! Thanks man!

Leave a Reply




Trackbacks

  1. Tutorial | Sending, manipulating and receiving data with sendAndLoad / AS3 « Flash Enabled Blog on June 5th, 2008 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 [...]