Using URLVariables()

By | January 30, 2008

URLVariables Class allow you to send or receive data from an URL encoded feed (with the data property of the URLRequest class).
There is so two possible use:

  1. Sending parameters to a server side script
  2. Read parameters written from a server side script

The use is quite simple, let’s start by building the PHP file that will receive the variables from a POST method and echo them again with a list of parameters association by name and value: email=”..”&id=”..”&db=”…”.

< ?php
 $psPreRegEmail=$_POST['sEml'];
 $FRM_ID=$_POST['sID'];
 $psBD=$_POST['sBD'];

 echo "email=".$psPreRegEmail;
 echo "&id=".$FRM_ID;
 echo "&db=".$psBD;
?>

Now we need to prepare our AS3 script that will send this 3 variables to our PHP file:

var request:URLRequest = new URLRequest("http://www.server.com/varsTest.php");
var variables:URLVariables = new URLVariables();//create a variable container
variables.sEml = email;
variables.sID = theForum;
variables.sBD = theDB;
request.data = variables;//add the data to our containers
request.method = URLRequestMethod.POST;//select the method as post
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.load(request);//send the request with URLLoader()

Here we send the variables and wait for the Event.COMPLETE, it will indicates us that the php files is totally downloaded (and so all the scripts has been executed). Now we can prepare our HandleComplete function:

function handleComplete(event:Event)
{
var loader:URLLoader = URLLoader(event.target);
var vars:URLVariables = new URLVariables(loader.data);
//Access data from the PHP file
trace("vars.email: "+vars.email);
trace("vars.id: "+vars.id);
trace("vars.db: "+vars.db);
}

That’s all!

Ahmet

 
1 Kudos
Don't
move!

2 thoughts on “Using URLVariables()

  1. Kitsu

    Hi

    I tried your example but instead of having the correct values with trace, I get the php code itself.
    That means I get:
    vars.id: “.$FRM_ID;
    echo “vars.db: “.$psBD;
    ?&gt;

    I know it comes from the php configuration on my server because I tested it on another server and it works, but I can’t find what I must change in my php.ini file to get the good results in trace();
    Do you have any idea? thanks

    Reply
  2. Marcel Doornbos

    Hi,

    I’m trying out Adobe AIR. and since there can’t be a database connection directly from there. I thought this could be something that could work. But.. I can’t seem to find out how to get this script working.

    Could you tell me how this script should look like in a HTML file? like with a form to create the variables that will be send to the server php file.(You can e-mail me at the e-mail which I put at mail in this comment)

    I hope you reply soon,

    Regards,
    Marcel Doornbos
    Devfine

    Reply

Thoughts?