PDA

View Full Version : Flash Input



zypher2004
03-25-2005, 06:11 AM
How can I input data into a flash animation through the URL like using a PHP get function.
thanks,
zypher2004

NTD
03-25-2005, 01:12 PM
Hi,

There are a few ways to import data into a flash movie. The most effiecient is the use of a load vars object...

new LoadVars()

Parameters

None.

Returns

Nothing.

Description

Constructor; creates an instance of the LoadVars object. You can then use the methods of that LoadVars object to send and load data.

Example

The following example creates an instance of the LoadVars object called myLoadVars:

myLoadVars = new LoadVars();


With the Load vars object, you have methods of load, onLoad, or onData. You can use those methods to ensure the data has loaded correctly.....


myLoadVars = new LoadVars();
myLoadVars.load("someFile.php");
myLoadVars.onLoad=function(success){
if(!success){
trace("Failed to load")
}else{
//do something with the loaded info
}
}

zypher2004
04-06-2005, 05:11 AM
could you please make a example .fla for me?
I use macromedia flash MX and I just want to print out the data from the URL into a .swf for now.
thanks,
zypher2004

NTD
04-06-2005, 05:38 AM
Hi,

Sending variables with the url itself to a PHP page would look like.....

http://someSite.com/myForm.php?name=NTD&email=webmaster@ntdesigns.net

The question mark symol tells the server and the script that everything that follows comprise variables. One thing to keep in mind is that the GET method has a 1024 character limit and is attached to the URL itself(viewable by all). The POST method attaches variable/name pairs to the header of the HTTP request (can't be seen) and doesn't have a character limit. I dont have a demo handy but it is not a difficult task. Create a PHP script to hold your variables and echo a trace back to flash saying successful or unsuccessful.

zypher2004
04-06-2005, 05:41 AM
Hi,

Sending variables with the url itself to a PHP page would look like.....

http://someSite.com/myForm.php?name=NTD&email=webmaster@ntdesigns.net

The question mark symol tells the server and the script that everything that follows comprise variables. One thing to keep in mind is that the GET method has a 1024 character limit and is attached to the URL itself(viewable by all). The POST method attaches variable/name pairs to the header of the HTTP request (can't be seen) and doesn't have a character limit. I dont have a demo handy but it is not a difficult task. Create a PHP script to hold your variables and echo a trace back to flash saying successful or unsuccessful.
I know PHP pretty well, and I know how to use $HTTP_GET_VARS['whatever'] to get the data from the URL but I want to print that data into my flash animation into a text fiend or something like that.
thanks,
zypher2004

NTD
04-06-2005, 07:32 AM
Hi,

You can use the sendAndLoad method of the loadVars object to send variables to a php script and return them to flash.



container = new LoadVars();
container.onLoad = function(success) {
if (!success) {
trace("Failed to load");
} else {
trace("Data is loaded");
trace(container.msg);
//populate the textfields with the returned data from the php script
_root.name.text = _root.container.name;
_root.myTextSave.text=_root.container.myTextSave;
_root.msg.text = _root.container.msg;
}
}
myButton.onRelease = function() {
//You could use the POST method to send the data(more secure), as long
//as the php script uses the GET method, it will return the url appended
//data correctly
_root.container.sendAndLoad("http://someSite.com/myReturn.php?name=NTD&myTextSave=This is some test text.", _root.container, GET);
}


The PHP script would look like.......



<?
$name=$_GET&#91;"name"&#93;;
$myTextSave=$_GET&#91;"myTextSave"&#93;;
echo &#40;"&msg=Connected to file!&name=$name&myTextSave=$myTextSave"&#41;;
?>



NTD

zypher2004
04-08-2005, 02:26 AM
i went out and bought macromedia flash just so I could do this and I dont understand how...:(
All i wat to do is go
http://www.mysite.com/animation.swf?name=bryan&lname=rolfe
and have it put that data into the animation.
thanks,
bryan rolfe

NTD
04-08-2005, 04:52 AM
Hi,

Purchasing Macromedia Flash is rather an expensive lesson just to send a variable from Flash to a PHP script and back. I hope the endeavor turns out to be worth your while. I am a fan of Flash and will help as much as I can. Flash is a very dynamic application and if your into graphic design, it is the tool of choice. Graphic design wise, there is nothing around that compares. File size and customization possibilities make Flash hard to compete with. When it comes to application development, Flash is not nearly as advanced or complete as visual basic, Java, C++, or other OOP programming languages, but it is making up ground quickly! O.K..... with my Macromedia Flash praises out of the way,.... back to sending and receiving variables to and from flash using PHP as the backend scripting language.......

Flash can use the "GET" method like this.....



container = new LoadVars&#40;&#41;;
container.onLoad = function&#40;success&#41; &#123;
if &#40;!success&#41; &#123;
trace&#40;"Failed to load"&#41;;
&#125; else &#123;
trace&#40;"Data is loaded"&#41;;
trace&#40;_root.container.myTextSave&#41;;
_root.name.text = _root.container.name;
_root.myTextSave.text=_root.container.myTextSave;
_root.msg.text = _root.container.msg;
&#125;
&#125;
myButton.onRelease = function&#40;&#41; &#123;
container.sendAndLoad&#40;"http&#58;//yourSite.com/myVarsTest.php?name=NTD&myTextSave=This is some test text.", _root.container, GET&#41;;
&#125;


Flash can use the "POST" method like this...



container = new LoadVars&#40;&#41;;
container.onLoad = function&#40;success&#41; &#123;
if &#40;!success&#41; &#123;
trace&#40;"Failed to load"&#41;;
&#125; else &#123;
trace&#40;"Data is loaded"&#41;;
trace&#40;_root.container.name&#41;;
_root.name.text = _root.container.name;
_root.myTextSave.text = _root.container.myTextSave;
_root.msg.text = _root.container.msg;
&#125;
&#125;;
myButton.onRelease = function&#40;&#41; &#123;
container.name = escape&#40;"NTD"&#41;;
container.myTextSave = escape&#40;"This is some test text."&#41;;
container.sendAndLoad&#40;"http&#58;//yourSite.com/myVarsTest.php", _root.container, POST&#41;;
&#125;



The PHP script would look something like(I am no PHP guru, if this is formatted incorrectly, ...... so be it)......


<?php
$name=$_POST&#91;"name"&#93;;
$myTextSave=$_POST&#91;"myTextSave"&#93;;
echo &#40;"&error=NONE&msg=Connected to file!&nameReturn=$name&textTest=&myTextSave"&#41;;
?>


Here is a zip file demo containing the fla and the PHP script. Upload the script to your site, and change the button file path from "yoursite.com" to your actual site path. The demo currently uses the POST method, but contains the "Get" method also. The "GET" method is commented out. Remove the comment marks and change the PHP script from POST to GET to test that method.

http://ntdesigns.net/quickDemo/sendAndLoad.zip

hope it helps
NTD