• home
  • forum
  • my
  • kt
  • download
  • A Simple XML-Based Searchable Database

    Author: 2007-06-13 15:16:54 From:

    calling php functions from flash

    This tutorial assumes that you already have expirience with PHP or other programming languages. In this tutorial I will introduce PHP¡¯s dynamic function construction in combination with Actionscript. This combination allows you to execute various tasks in Flash with only one PHP script.

    note that this is a more theoretical explanation, it¡¯s not a step by step tutorial. I thik it¡¯s the better approach because the main goal of this tutorial is to show you one key PHP feature. The rest is up to you. Once you understand it, the possibilities are endless.

    Assume you have a quiz application with PHP as middle layer with featues like login screen, questions, summary etc. You need to connect to a database to check the passwords, to load new questions etc. The usual way to develop this is to make sparate PHP scripts for login, etc. The more features you have in you flash app, the number of PHP scipts will grow that handle the special task.

    Get ready

    Well, ok, maybe you will be happy if your app uses only 3-5 scripts. But the other approach could be to use only one PHP script with separate functions that handle some tasks. The functions will be called from flash!

    I will demonstrate a simple example and hope you get the idea:

    Assume you have you script called myscript.php, the script contains the following function:

    <?php
    function say_hello_to_flash(){
     echo ¡°&message=Hi, my name is string and I¡¯m coming from PHP, and you?¡±;
    }
    ?>
    

    On the other side you have your swf file residing in the same folder. Put a dynamic textfield on the stage and name it ¡°message¡±.

    Now, what about PHP and the function? How does it work?

    Well, before the final step I will try to explain to PHP¡¯s dynamic function names. Assume you have a usual PHP function:

    <?
    function scream(){
     echo ¡°HELLO!!!!¡±;
    }
    ?>
    

    So if you wanted to call this function, you will normally write:

    <?
    scream();
    ?>
    

    and you function will be executed.

    But, take a look at this:

    <?
    $my_func_name = ¡°scream¡±;
    $my_func_name();
    ?>
    

    What happened?

    The last line of code was executed like

    <?
    scream();
    ?>
    

    because $my_func_name has the value ¡°scream¡±!

    OK, I understand, but what about flash?

    Let¡¯s come back to our flash example. If you want to execute the function say_hello_to_flash(), you just have to construct a string with the value like the function name.

    So in the first frame we write the following actionscript:

    my_func_name = ¡°say_hello_to_flash¡±;
    

    and then send the string to myscript.php:

    loadVariablesNum(¡°myscript.php¡±, 0, ¡°GET¡±);
    

    Now, PHP will receive the string my_func_name. To call the function you have to ¡°construct¡± the function call using the flash string:

    $my_func_name();
    

    That the basic idea, I used this method developing a quiz app. The function names were:

    check_user, send_question, show_summary etc.

    Cao
    Mirza

    » Level Advanced

    Added: : 2002-09-17
    Rating: 5.38 Votes: 89
    Hits: 1337
    » Author
    Mirza Hatipovic is freelance developer based in Bosnia and Hercegovina. He uses PHP, Flash, ColdFusion and XML to create dynamic web content.
    » Download
    Download the files used in this tutorial.
    Download (0 kb)

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      3D (20)
      Math Physics (14)
      3rd Party (5)
      Navigation (60)
      Actionscripting (26)
      Optimization (16)
      Animation (32)
      Projector (9)
      Audio (46)
      Special Effects (112)
      Backend (25)
      Text Effects (65)
      Drawing (18)
      Tips and Techniques (41)
      Dynamic Content (25)
      Tricks (6)
      Games (66)
      Utilities (19)
      Getting Started (71)
      Video (10)
      Interactivity (21)
      Web Design (22)

    New

    Hot