Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Flash CS3 and PHP

This is a discussion on Flash CS3 and PHP within the Tutorials forums, part of the Flash English category; Here is a very interesting subject, not that the others seen so far aren"t, but this time we make ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  7 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 03-10-07, 19:51
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Flash CS3 and PHP

Here is a very interesting subject, not that the others seen so far aren"t, but this time we make Flash CS3 communicate with the server.
What"s changed in order to send/receive variables from server side scripts are quite a few.
First of all, the AS2 LoadVars class has been removed and is not longer used by AS3.
Here is an example on how to behave with Actionscript 3.0"
I create an FLA and save it as " load_vars.fla " . I create the Document Class, an AS file that I save as " ExLoadVars.as " , like so:
Code:
package
{
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.net.URLVariables;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoaderDataFormat;
	import flash.text.TextField;
	import flash.display.SimpleButton;
	import flash.events.*;
	
	public class ExLoadVars extends MovieClip
	{
		public function ExLoadVars()
		{
			addButtonListener();
		}
		
		private function addButtonListener():void
		{
			board_mc.invia_btn.addEventListener(MouseEvent.MOUSE_DOWN,inviaDati);
		}
		
		private function addListeners(d:IEventDispatcher):void
		{
			d.addEventListener(Event.OPEN,inizio);
			d.addEventListener(ProgressEvent.PROGRESS,inProgresso);
			d.addEventListener(Event.COMPLETE,completato);
			d.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityError);
			d.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatus);
			d.addEventListener(IOErrorEvent.IO_ERROR,ioError);
		}
		
		private function inizio(e:Event):void 
		{
			debug_txt.appendText('script chiamato: duplicanumero.php\n');
			debug_txt.appendText('inizio: \n');
		}
		
		private function inProgresso(e:ProgressEvent):void 
		{
			debug_txt.appendText('percentuale caricata: '+e.bytesLoaded+' totale: '+e.bytesTotal+'\n');
		}
		
		private function completato(e:Event):void
		{
			var loader:URLLoader=URLLoader(e.target);
			debug_txt.appendText('completato: '+loader.data+'\n');
			
			var vars:URLVariables=new URLVariables(loader.data);
			board_mc.answer_txt.text=(vars.answer).toString();
			debug_txt.appendText('La risposta è '+vars.answer+'\n');
		}
		
		private function securityError(e:SecurityErrorEvent):void 
		{
			debug_txt.appendText('errore sicurezza: '+e+'\n');
		}
		
		private function httpStatus(e:HTTPStatusEvent):void 
		{
			debug_txt.appendText('errore HTTP: '+e+'\n');
		}
		
		private function ioError(e:IOErrorEvent):void 
		{
			debug_txt.appendText('Errore in invio/caricamento: '+e+'\n');
		}
		
		private function inviaDati(m:MouseEvent):void
		{
			debug_txt.text='';
			var NumeroInserito:Number=Number(board_mc.insert_txt.text);
			var variables:URLVariables=new URLVariables('numero='+NumeroInserito);
			var richiesta:URLRequest=new URLRequest();
			richiesta.url='http://www.flepstudio.org/swf/duplicanumero.php';
			richiesta.method=URLRequestMethod.POST;
			richiesta.data=variables;
			var loader:URLLoader=new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.VARIABLES;
			addListeners(loader);
			try 
			{
				loader.load(richiesta);
			} 
			catch (error:Error) 
			{
				trace('impossibile caricare la richiesta');
			}
		}
	}
}
Try my example here:







The PHP script called (duplicanumero.php) is very simple:
PHP Code:
<"php
$num=$_POST["
numero"];
$answer=$num*2;
echo ("
answer=".$answer);
"

Let"s analyze Actionscript:
The three functions ( methods ) worth commenting on are inviaDati(), addListeners() and completato() .

InviaDati
With AS2 we were used to initiate the LoadVars class, associate some properties to it and send it via POST or GET.
As already mentioned, the LoadVars class has been removed, so here is the way to obtain the same result ( much more straightforward and logical than AS2, as it shows exactly what happens ):

I create a variable to hold the value I want to send to the PHP script
var NumeroInserito:Number=Number(board_mc.insert_txt.t ext);
I initialize the URL Variables class and I pass the above variable to it:
var variables:URLVariables=new URLVariables('numero='+NumeroInserito);
I initialize the URLRequest class to " prepare " Flash " that is about to send data via http
var richiesta:URLRequest=new URLRequest();
I define the request"s URL
richiesta.url='http://www.flepstudio.org/swf/duplicanumero.php';
I specify the sending method (POST in this case)
richiesta.method=URLRequestMethod.POST;
I assign the value to send contained in the variables variable to the .data property of the richiesta variable
richiesta.data=variables;
initialize the URLLoader class
var loader:URLLoader=new URLLoader();
I tell loader to format the string for the data transfer ( as if it were "numero=")
loader.dataFormat=URLLoaderDataFormat.VARIABLES;
I add the necessary listeners
addListeners(loader);
and I call the PHP script
try
{
loader.load(richiesta);
}
catch (error:Error)
{
trace('impossible to load the request');
}

addListeners
nothing new in this method of the ExLoadVars class; I just add the listeners for each event I need to monitor during the call to the PHP script

completato
This method is executed when Flash has completed the data transfer, since a listener is listening ( addListeners ), as we process the data returned by PHP, like so:
I initialize the URLLoader class passing the event"s .target property to it
var loader:URLLoader=URLLoader(e.target);
I initialize the URLVariables class passing the loader"s .data property to it
var vars:URLVariables=new URLVariables(loader.data);
I load the value returned by the PHP script into the text field, creating a vars" property with the same name as returned by the PHP script
board_mc.answer_txt.text=(vars.answer).toString();

Nice uhu " ;)

Source files:
Attached Files
File Type: zip FlashCS3+PHP.zip (8.2 KB, 120 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-08-08 at 06:32..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 31-12-07, 04:16
Junior Member
 
Join Date: Dec 2007
Posts: 6
Rep Power: 0
mxmania is on a distinguished road
Re: Flash CS3 and PHP

cant find the source files
thx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 31-12-07, 08:01
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Flash CS3 and PHP

Hi,

go here:
http://www.flepstudio.org/forum/down...?do=file&id=42
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 23-01-08, 23:08
Junior Member
 
Join Date: Dec 2007
Posts: 3
Rep Power: 0
returnButton is on a distinguished road
Re: Flash CS3 and addEventListener variables

Hi, thanks for this, very interesting & I'm sure I will use it a lot!
I have a question about listeners.. I've always found them limiting, maybe you can clear this up for me...

so this calls a function called inviaDati:

addEventListener(MouseEvent.MOUSE_DOWN,inviaDati);

but can I pass variables in? and if so how do I get the function to receive them?

eg:

something.addEventListener(MouseEvent.MOUSE_DOWN, inviaDati, var1, var2);

function inviaDati(e:Event, var1:String, var2:String)
{
}

Hope this makes sense! thanks..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 25-01-08, 09:18
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Flash CS3 and addEventListener variables

Quote:
Originally Posted by returnButton View Post
Hi, thanks for this, very interesting & I'm sure I will use it a lot!
I have a question about listeners.. I've always found them limiting, maybe you can clear this up for me...

so this calls a function called inviaDati:

addEventListener(MouseEvent.MOUSE_DOWN,inviaDati);

but can I pass variables in? and if so how do I get the function to receive them?

eg:

something.addEventListener(MouseEvent.MOUSE_DOWN, inviaDati, var1, var2);

function inviaDati(e:Event, var1:String, var2:String)
{
}

Hope this makes sense! thanks..
Hi...
I think you can't pass variables.
You should add some variable at the start of the script and store them the values you need
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 30-01-08, 05:05
Junior Member
 
Join Date: Dec 2007
Posts: 15
Rep Power: 0
lex_ph is on a distinguished road
Re: Flash CS3 and PHP

ei Flep, do you have the English version for this one? Thanks!~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 30-01-08, 07:45
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Flash CS3 and PHP

Hi lex, sorry I did not get you.
Which one ?
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-02-08, 01:26
Junior Member
 
Join Date: Dec 2007
Posts: 15
Rep Power: 0
lex_ph is on a distinguished road
Re: Flash CS3 and PHP

The code above...I don't get some of the parts because it is not in English...I am noob in AS and it is important for me if the code above is written in English...

Anyway, if there's no English translation, it's ok...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-02-08, 08:22
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Re: Flash CS3 and PHP

I have translated the code :
Code:
package
{
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.net.URLVariables;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLLoaderDataFormat;
	import flash.text.TextField;
	import flash.display.SimpleButton;
	import flash.events.*;
	
	public class ExLoadVars extends MovieClip
	{
		public function ExLoadVars()
		{
			addButtonListener();
		}
		
		private function addButtonListener():void
		{
			board_mc.invia_btn.addEventListener(MouseEvent.MOUSE_DOWN,inviaDati);
		}
		
		private function addListeners(d:IEventDispatcher):void
		{
			d.addEventListener(Event.OPEN,onStart);
			d.addEventListener(ProgressEvent.PROGRESS,onProgress);
			d.addEventListener(Event.COMPLETE,onComplete);
			d.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityError);
			d.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatus);
			d.addEventListener(IOErrorEvent.IO_ERROR,ioError);
		}
		
		private function onStart(e:Event):void 
		{
			debug_txt.appendText('script chiamato: duplicanumero.php\n');
			debug_txt.appendText('onStart: \n');
		}
		
		private function onProgress(e:ProgressEvent):void 
		{
			debug_txt.appendText('percentuale caricata: '+e.bytesLoaded+' totale: '+e.bytesTotal+'\n');
		}
		
		private function onComplete(e:Event):void
		{
			var loader:URLLoader=URLLoader(e.target);
			debug_txt.appendText('onComplete: '+loader.data+'\n');
			
			var vars:URLVariables=new URLVariables(loader.data);
			board_mc.answer_txt.text=(vars.answer).toString();
			debug_txt.appendText('La risposta è '+vars.answer+'\n');
		}
		
		private function securityError(e:SecurityErrorEvent):void 
		{
			debug_txt.appendText('errore sicurezza: '+e+'\n');
		}
		
		private function httpStatus(e:HTTPStatusEvent):void 
		{
			debug_txt.appendText('errore HTTP: '+e+'\n');
		}
		
		private function ioError(e:IOErrorEvent):void 
		{
			debug_txt.appendText('Errore in invio/caricamento: '+e+'\n');
		}
		
		private function inviaDati(m:MouseEvent):void
		{
			debug_txt.text='';
			var insertedNumber:Number=Number(board_mc.insert_txt.text);
			var variables:URLVariables=new URLVariables('numero='+insertedNumber);
			var request:URLRequest=new URLRequest();
			request.url='http://www.flepstudio.org/swf/duplicanumero.php';
			request.method=URLRequestMethod.POST;
			request.data=variables;
			var loader:URLLoader=new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.VARIABLES;
			addListeners(loader);
			try 
			{
				loader.load(request);
			} 
			catch (error:Error) 
			{
				trace('impossibile caricare la request');
			}
		}
	}
}
__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-05-08, 16:25
Junior Member
 
Join Date: May 2008
Posts: 1
Rep Power: 0
alice is on a distinguished road
Re: Flash CS3 and PHP

thx dude~ lov it,i searched it from google for so long,
this are the most complete that i need to refer.
But the bad happened is i can't download the file,
because i'm new member here.....disapointed,
anyone can help me? need it urgent...... pls!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:44.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap