• 3rd November 2008 - By Bill Nunney
    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

  • 7 Comments to “AS3 Basics: Sending Variables from AS3 to PHP”

    • Ryan on 12 February, 2009

      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 on 13 May, 2009

      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!

    • Walmik on 14 May, 2009

      Thanks… this makes it simpler than in AS2!

    • Bob on 15 May, 2010

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

      • Bill Nunney on 16 May, 2010

        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.

    • pell grant on 4 July, 2010

      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 on 23 August, 2010

      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!!!

    Leave a Reply