passing variable from php into flash, help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

passing variable from php into flash, help please

Post by ConVit »

Here are what I'm having:

<?PHP

$testVal = "This is a test";

$test = urlencode($testVal);

$out = "test=". $testVal;
echo $out;

?>

// and this is my flash file
function getData() {

// Prepare request

var urlReq:URLRequest=new URLRequest("http://localhost/test.php");
urlReq.method=URLRequestMethod.GET;

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

}

function completeHandler(evt:Event) {

var vars:URLVariables = URLVariables(evt.target.data);
trace(" TEST = ", vars.test);
trace(" TEST = ", unescape(vars.toString()));
}

// and below is the ouput
TEST = undefined
TEST = test=This is a test

/////////////////////////////////////

I've tried with several different ways to get ONLY the value of the variable - that means I only want to have "This is a test" returned, but I either got "undefined" or the whole thing "test=This is a test", instead.

Please help. I'm kind of new to PHP and FLASH (CS4).

Thanks
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: passing variable from php into flash, help please

Post by jazz090 »

its been a while since i worked with string vars in AS (i mostly use XML now because of better support, query strings are so AS2 but as far as i know, they are still supported in AS3) and i can only assume that there is something wrong with this code. my suggestion to you is to break up the query string using a simple RegEx command.
ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

Re: passing variable from php into flash, help please

Post by ConVit »

Thank you for your advice, but could you please give me some example. I'm new to both PHP and Flash.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: passing variable from php into flash, help please

Post by jazz090 »

this is what i wrote just now:

Code: Select all

var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("http://127.0.0.1/sandbox.php");
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, display);
loader.load(req);
function display(e:Event):void{
    trace(e.target.data.q);  // test
    trace(e.target.data.hl); // en
}
sandbox.php is a static file with this in it: q=test&hl=en (similar to google) and this works fine, if you want me to explain how to do it with RegEx then please say so.
ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

Re: passing variable from php into flash, help please

Post by ConVit »

Thank you very much. But I don't know how you declare the q, test, hl, en, in the .php file.
Please show me, and explain what/why it is. Thanks
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: passing variable from php into flash, help please

Post by jazz090 »

i told you, its a basic static file, i.e. it JUST contains "q=test&hl=en" explicitly defined. now if you want to make that dynamic, you can define them like this and its equal to that.

this is what you would do for dynamic pages:

Code: Select all

<?php
$query = "test";
$language = "en";
echo "q=".$query."&hl=".$language;
?>
when i say its static, that means content is defined. i.e. it may as well be a .txt file. imagine opening notepad and typing "q=test&hl=en" in it and saving as sandbox.txt
you can access this with the code i gave you, just change the extension to .txt. like i said the problem wasnt your data source, your data source was absolutely defined in the correct way. you just had your AS3 mixed up. you can have a look at sandbox.php as i have attached it.
Attachments
sandbox.zip
sandbox.php
(132 Bytes) Downloaded 12 times
ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

Re: passing variable from php into flash, help please

Post by ConVit »

Thank you.

I just wonder if you tested it. Here is what I got:

undefined
en

If I echoed only one, then I got Error. If echoed both, then that's what I got
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: passing variable from php into flash, help please

Post by jazz090 »

if you echoed only one variable, make sure you remove the ampersand at the end so have it like this: "q=test" and NOT like this "q=test&" because i just double checked my script and it works absolutely fine, when you add the extra amp at the end, i throws the error. i can recommend that you make advantage of php arrays for this:

Code: Select all

<?php
$vars = array("q" => "test", "hl" => "en", "time" => time());
foreach ($vars as $var => $value){
    echo $var."=".urlencode($value);
    if (end($vars) != $value){
        echo "&";
    }
    //q=test&hl=en&time=1252233053
}
unset($var, $value);
?>
ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

Re: passing variable from php into flash, help please

Post by ConVit »

Thank you very much for your patience. You've helped me a lot.

It works fine with browsing the .php file:

q=test&hl=en&time0=1252242220

///////////////////////////////////////////////
But, for the Flash:

trace(e.target.data.q); // undefined
trace(e.target.data.hl); // en
trace(e.target.data.time);1252241972

It works great if the first var is used as a fake var:

//trace(e.target.data.q);
trace(e.target.data.hl);
trace(e.target.data.time);

Output:
en
1252242220
////////////////////////////////////////////
I'll keep learning this to understand why and how it is.

Again, thank you very much for your GREAT helps.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: passing variable from php into flash, help please

Post by jazz090 »

i dont know what this keeps happening for you, you should not need a fake variable to make this work, it works fine on this computer, if you want to upload your fla file and your php file, i can take a close look for you.
ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

Re: passing variable from php into flash, help please

Post by ConVit »

Thank you,

I used the .fla file you post on this for me
ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

Re: passing variable from php into flash, help please

Post by ConVit »

var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("http://localhost/test.php");
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, display);
loader.load(req);
function display(e:Event):void{
trace(e.target.data.q); // test
trace(e.target.data.hl); // en
trace(e.target.data.time);
}


/////////////////////////////////////////////////////////////////////


<?PHP
$vars = array("q" => "test", "hl" => "en", "time0" => time());
foreach ($vars as $var => $value){
echo $var."=".urlencode($value);
if (end($vars) != $value){
echo "&";
}
//q=test&hl=en&time=1252233053
}
unset($var, $value);
?>

//////////////////////////////
undefined
en
1252249518
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: passing variable from php into flash, help please

Post by jazz090 »

ok try this now: rename test.php to another name and then change the variables inside or add your own using the array.
then update the AS code to match the new name of test.php and try to trace the new vars you added. let me know if this solves it.
ConVit
Forum Newbie
Posts: 9
Joined: Sat Sep 05, 2009 6:13 am

Re: passing variable from php into flash, help please

Post by ConVit »

var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("http://localhost/conga.php");
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, display);
loader.load(req);
function display(e:Event):void{
trace(e.target.data.q);
trace(e.target.data.hl);
trace(e.target.data.myTime);
}


$vars = array("q" => "The Q", "hl" => "English", "myTime" => time());
foreach ($vars as $var => $value){
echo $var."=".urlencode($value);
if (end($vars) != $value){
echo "&";
}
//q=test&hl=en&time=1252233053
}
unset($var, $value);

// The same
undefined
English
1252257449

///////

Thank you very much for your kindness and patience.
If everything worked fine on your computer, then I think there might be something abnormal in my server
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: passing variable from php into flash, help please

Post by jazz090 »

if that is the case then just use the dummy variable like you suggested.
Post Reply