AS3 Basics: Sending Variables from AS3 to PHP

Flash CS3 Professional

Hello and welcome to another quick AS3 tutorial. As with any client-side application, eventually you’ll want to comunicate with a server, for instance to display scores and such. This is something that Flash has been able to do quite a few versions back, but some of you may not know what the AS3 equivalent is. So Here we go.

In AS3 we use the URLVariables class to store our intended variables like so:

first we instantiate the myVariables var with the URLVariables class.

var myVariables:URLVariables = new URLVariables();

Then we create and define properties within the myVariables instance (these are the names we will call in PHP to access the data).

myVariables.myName = "Bill";
myVariables.beans = "beans are awesome!";

We now have to create an instance of the URLRequest class in order to send our variables.

var myURLRequest:URLRequest = new URLRequest("http://www.mywebsite.com/phpvarstutorial.php");

Now for the magic that makes it all work…

myURLRequest.data = myVariables;
navigateToURL(myURLRequest, '_blank');

Basically what we are doing there is attaching our URLVariables to our URLRequest as data to be transmitted along to the target URL. And then using the navigateToURL function to send everything (and also bring up the page).

You can then use $_GET['var']; in PHP to retrieve the variables.

See our PHP Basics : Get Command – Using URL Queries & Variables tutorial to learn how to read the passed variables.

Example files here (PHP included):

Thanks for reading

Bill

Tags: , , ,

  • http://www.hungateagency.com Ryan

    Thanks for the info… I have been looking all over and to know now that its really simple to send/receive, it has made my day.

  • Chandler Savage

    You are a life saver! I am making a Psychology experiment using Flash CS3 and have been having trouble sending the data to PHP and then to MySQL. For whatever reason, the AS3 examples for this topic is really lacking. Thanks again for the great post. Your code worked like a charm!

  • http://www.walmik.info Walmik

    Thanks… this makes it simpler than in AS2!

  • Bob

    Is it easy to change this from a get to a POST?

  • Bill Nunney

    Yes you can quite easily, this tutorial is a bit short. I should really update it.

    anyway:

    import flash.net.URLRequestMethod;

    var myURLRequest:URLRequest = new URLRequest(“http://www.mywebsite.com/phpvarstutorial.php”);

    myURLRequest.method = URLRequestMethod.POST;

    then you would use the php function

    $_POST['var']

    to get each variable.

  • http://www.yourpellgrants.org pell grant

    This is such a great resource that you are providing and you give it away for free. I enjoy seeing websites that understand the value of providing a prime resource for free. I truly loved reading your post. Thanks!

  • Francisco

    Hi,

    This seems like the exact turorial I beed but the download isn’t working at all. Is it possible to re-upload it please? This is the link I get:
    http://www.wuup.co.uk/wp-content/uploads/2008/11/phpvarstutorial.zip
    and the message is a 404 error.

    This looks extremely useful. I’ve been trying to get data to transmit from AS3 to PHP. I’ve achieved it in AS2 and PHP before but AS3 is proving tobe difficult. PLEASE HELP!!!

  • http://www.e2-projects.com Johan

    If you don’t want a blank window to open but do want to sent the vars thisl work quite nice.

    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;

    var myVariables:URLVariables = new URLVariables();
    myVariables.myName = “Bill”;
    myVariables.beans = “beans are awesome!”;

    var request:URLRequest = new URLRequest(‘form.php’);
    request.method = URLRequestMethod.POST;
    request.data = myVariables;
    loader.load(request);

  • / Dimension

    Hi, to load the posted value into a variable

    $RandomVariable = $_POST['myName']; (for your example)

    If it is then for some reason it won’t work :S

    Good tutorial though

  • http://www.wuup.co.uk Alan Hamlyn

    Hey, did you set this up to work with a form? Self submitting etc.

    You can do a little get test too.

    Then type in the URL to file. i.e http://blah.com/file.php?myName=Alan

  • Charlotte

    please help me…….

    (php code)

    (as3 code)
    var request:URLRequest = new URLRequest(“http://localhost/2.php”);
    request.method = URLRequestMethod.GET;

    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.addEventListener(Event.COMPLETE, completeHandler);
    loader.load(request);

    function completeHandler(evt:Event) {

    var count1 = evt.target.data.count;
    trace (count1);
    }

    (PROBLEM)
    i’m trying to count how many text file inside “txt” folder.
    In this program $count contains the no. of .txt in the “txt folder” and in php $count successfully return the no. of .txt file….
    my purpose is to trace the no. of .txt file in a as3…
    but an error occured stated “undefined”… help me please…

    Thank you in advance…blessGod

  • Charlotte

    can i ask for a program that contains:

    1. php that count number of .txt file in a folder
    2. as3 that read the variable that count number of .txt file in a folder in php

    Thank’s a lot :0)