Home » ActionScript 3.0, Flash CS5, PHP

Send / Pass values from Flash as3 to PHP without opening new window

18 September 2011 2 Comments
Share on Facebook

I was trying to pass as3 data variables / values from Flash as3 to PHP. I know it is simple to make new request through navigateToURL(), it executes well and passes the data to php and runs the script but opens new browser window with url query variables. So this will not work when we wanted to pass data silently to serverside scripts from Flash.

Remember what you need to run:

  • Flash CS3 – CS5
  • PHP

send data as3 to php

 

Method 1: Passing varaibles from Flash as3 to PHP:

one way of sending values from Flash actionscript 3.0 to PHP call by making request through navigateToURL(). But keep in mind,

  • It open new browser window if you use “_blank” and navigates from flash content on using “_self”
  • It returns value from PHP to Flash again.
  • Use this method to pass data on query string.

Actionscript code to send data to php:

import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.navigateToURL;

var urlVariables:URLVariables = new URLVariables;
urlVariables.userName = "test_user";
urlVariables.password = "testpass";

var urlRequest:URLRequest = new URLRequest("http://localhost/retrive.php");
urlRequest.data = urlVariables;

navigateToURL(urlRequest, "_self")

Note: the parameters of navigateToURL():

urlRequest:URLRequest — A URLRequest object that specifies the URL to navigate to.

  • “_self” specifies the current frame in the current window.
  • “_blank” specifies a new window.
  • “_parent” specifies the parent of the current frame.
  • “_top” specifies the top-level frame in the current window.

PHP code to receive data to php:

We are receiving the data passed from Flash using the $_GET[] method.

<?php

$userName = $_GET["userName"];
$pass = $_GET["password"];

echo $userName."::".$pass;

?>

Note: The variable name inside $_GET[] should match the URL variable we have added in AS3.

Method 2: Passing varaibles from Flash as3 to PHP silently:

This method will makes call to server side script using method sendToURL() but ignores response.

Use this method when you need to pass data silently from flash to PHP without opening new window.

Actionscript code to send data without opening new window:

import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.navigateToURL;

var urlVariables:URLVariables = new URLVariables;
urlVariables.userName = "test_user";
urlVariables.password = "testpass";

var urlRequest:URLRequest = new URLRequest("http://localhost/retrive.php");
urlRequest.data = urlVariables;

sendToURL(urlRequest);

Note: This does not have parameter to specify target Window.

PHP script is the same for both. Download the source files contain Flash and PHP script. This is simple way of passing the data from Flash actionscript 3.0 to PHP script.

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


2 Comments »

  • send pass data from flash as3 to php without new window … | App Flash said:

    […] den Originalbeitrag weiterlesen: send pass data from flash as3 to php without new window … This entry was posted in Live Videos and tagged as3-script, new-window, pass-data, php-script, […]

  • Misterhee said:

    It should also be noted that if you need data back (method 2 will ignore any server response), that you can use something like the below:import flash.events.Event;import flash .net .*;var myRequest:URLRequest = new URLRequest(“myscri…”);var myLoader:URLLoader = new URLLoader();var myVariables:URLVariables = new URLVariables();myVariables.firstProperty = “first”;myVariables.secondProperty = “second”;myRequest.method = URLRequestMethod.GET;myRequest.data = myVariables;function onLoaded(evt:Event):void { trace(“here we get the data back: “+myLoader.data);}myLoader.addEventListener(Event.COMPLETE, onLoaded);myLoader.load(myRequest);// Have JSON data etc to send ? Then set the content type and POST the data:import flash.events.Event;import flash.net.*;var myRequest:URLRequest = new URLRequest(“myscript .php””);var myLoader:URLLoader = new URLLoader();var myXML:XML = “Hello World”;myRequest.contentType = “text/xml”;myRequest.data = myXML;function onLoaded(evt:Event):void { trace(“here we get the data back: “+myLoader.data);}myLoader.addEventListener(Event.COMPLETE, onLoaded);myLoader.load(myRequest);

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.