turrier.fr

Source : ct|28.02.09

< Tutorials Computer, Multimedia, Chinese

Run a PHP file since a Flash animation

We are going to use a simple and concrete method allowing to run a php file since a running flash animation. The ActionScript loadVariablesNum() method allows an action script program to call a php file, passing him variables, then to get back return variables from this php file. In the following sample, we will a very simple flash animation, consisted of a single frame (image 1). This image contains 2 dynamic text fields (mavar1 and mavar2) and 4 lines of code. The image 1 displays the text "Bonjour!" contained in the variable mavar1. Then it calls the file " essai.php " to whom it passes the value of this variable. It receive in return a value $variable2 passed in the variable mavar2 which it displays immediately.

image0

We are going to follow the following steps:
1) Create a flash project source file "essai.fla";
2) Create the corresponding flash executable file "essai.swf";
3) Create the file "essai.html" containing the flash animation "essai.swf";
4) Create the file "essai.php" which will be called by the executable flash file "essai.swf";
5) Test the result.

1) Create a source file essai.fla

To create the project source file "essai.fla", rather with Flash 8 (or more), it is enough to :
1) Create a new flash document (Fichier/Nouveau);
2) Set the dimension and the background color of the flash animation flash (Modification/ document, 250p x 100px, red background for example);
3) create, With the tool text, two dynamic text fields named mavar1 and mavar2;
4) Put the following code in the image 1 (right click on image1 then Action).

mavar1="bonjour !"
mavar2="";
loadVariablesNum("essai.php", 0, "POST");
stop();

5) Save the source file "essai.fla" (Fichier/Enregistrer sous/essai.fla).

image1

2) Create the executable file "essai.swf"

With Flash 8 (or more) :
1) Generate the executable file essai.swf. For this make "Fichier/Paramètres de publication", then "Fichier/Publier".

3) Create the file "essai.html"

The source code of the essai.html file is the following one :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>essai</title>
</head>
<body>
<object type="application/x-shockwave-flash" data="essai.swf" width="250" height="100">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="essai.swf" />
</object>
</body>
</html>

4) Create the file "essai.php"

The source code of the essai.php file is the following one :

<?
$variable1= $_POST['mavar1'];
$variable2= "Le retour est $variable2";
echo "mavar2=$variable2";
?>

The first line allows to get back the contents of the flash variable mavar1 in the php variable $variable1;
The second line allows to initialize the php variable $variable2;
The third line allows to send the contents of the php variable $variable2 to the flash variable mavar2.

5) Test the result

Transfer the three files essai.html, essai.php and essai.swf in a directory essai ( for example ) situated in the root of your php compatible Web site (http://monsite.fr/essai for example). Since the google address bar call the page http://monsite.fr/essai/essai.html.

image2

If you do not have a compatible php Web site, use EasyPHP (for example) to test the result on your computer.


Valid XHTML 1.0 Transitional

© http://turrier.fr (2007)