Jesse lives and works in Melbourne Australia. He is the Cofounder of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse's main focus nowadays is managing http://ActionScript.org, but he enjoys participating actively in community and the wider Flash scene when he has time. To see this in action, type a value in the variable field below and press 'Send'.
The source for this tutorial includes the PHP files I used in my example above as well as the traditional FLA sources.
Before we being, a few words on PHP. PHP is a Server Side scripting tool which allows you to process data before it's seen by the end user. Basically it allows you to create dynamic HTML pages. You could just as easily use CGI, ASP or JSP to achieve the outcome we're after in this tutorial, but I chose PHP because it's (a) Free and easy (b) What I know :o)
The First File (pass.fla)
OK, download the source and you will see that my first Flash file is called pass.fla. Inside pass.fla is a simple imput text field with the variable name 'value' and a button with this code:
[as]on (release) {The important line above is the getURL command. So let's break it down:
The Second File (middle.php)
This file contains a lot of junk to pad it out but the most important line is line 12 which reads:
click <a realsrc="end.php?value=<?php echo $value; ?>" href="http://www.actionscript.org/dev/admin/end.php?value=<?php echo $value; ?>">here</a> to end your variable back
As you can all see this is a standard HTML link with some funny jargon added. The jargon between the <? and ?> tags is PHP and all it says is "Fetch the vale variable and write it's contents here". So if I were to have types 'test' in the pass.swf file above, this line would be outputted as:
click <a realsrc="end.php?value=test" href="http://www.actionscript.org/dev/admin/end.php?value=test">here</a> to end your variable back
Understand? The one important thing to note is that if you change the name of the variable you are passing around, you need to change the name in the call. So if I were to change the input field variable to "abcd" my code should be:
click <a realsrc="end.php?abcd=<?php echo $value; ?>" href="http://www.actionscript.org/dev/admin/end.php?abcd=<?php echo $value; ?>">here</a> to end your variable back
So, when clicked this link will load up end.php, and using the GET method (they way we have included the variable in the URL is the traditional HTML form of the GET method), it passes our variable across.
The last file is discussed overleaf...
The final piece of the puzzle is the end.fla and end.php combination. If we assume I originally passed the value "test", by the time I get to the end.php file the URL will be something like:
www.site.com/end.php?value=test
Now we have the value variable inside the end.php page but we need to get it into the Flash file within end.php. Luckily, Macromedia are consistently awesome so they made Flash compatible with the GET variable method. If I have an swf file online (it only works online, not on your hard-drive) called example.swf, and I open it in my browser by typing: example.swf?variable=1234 , Flash recognizes that extra information as a passed variable and creates a variable within the example.swf file called 'variable' which holds the string "1234". So we're going to exploit this feature here.
As most of you would know, when you embed your flash file in an HTML file you use HTML <object> and <embed> tags. Each of these tags makes reference to the swf file you wish to embed, so all you have to do is include the value you wish to pass by adding "?variable=1234". Since we are passing a dynamic variable though, we use dynamic code which looks like this:
<param name=movie value="end.swf?value=<?php echo $value; ?>">
<embed realsrc="end.swf?value=<?php echo $value; ?>" src="http://www.actionscript.org/dev/admin/end.swf?value=<?php echo $value; ?>" quality=high bgcolor=#FFFFFF ...>
Here, as before I have use the PHP code which states "Insert the contents of the value variable here". So if I had passed the value "test" these lines will be processed as:
<param name=movie value="end.swf?value=test">
<embed realsrc="end.swf?value=test" src="http://www.actionscript.org/dev/admin/end.swf?value=test" quality=high bgcolor=#FFFFFF ...>
This code is just like our example.swf example above. The question-mark indicates we're passing a variable, the variable is called 'value' and it's value is "test".. Flash reads all this in and as a result inserts the string "test" in the value text field in end.swf.
I've done my best to explain this one but it gets kind of messy along the way. Checkout these relevant threads on the forums and the source code to become familiar with the method.
http://www.actionscript.org/forums/showthread.php3?threadid=743
http://www.actionscript.org/forums/showthread.php3?threadid=903
If you have more questions, post them on the forums as usual. Cheers.
| Jesse Stratford [email:jessestratford@actionscript.org] is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |