passing variable from php into flash, help please
Moderator: General Moderators
passing variable from php into flash, help please
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
<?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
Re: passing variable from php into flash, help please
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.
Re: passing variable from php into flash, help please
Thank you for your advice, but could you please give me some example. I'm new to both PHP and Flash.
Re: passing variable from php into flash, help please
this is what i wrote just now:
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.
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
}Re: passing variable from php into flash, help please
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
Please show me, and explain what/why it is. Thanks
Re: passing variable from php into flash, help please
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:
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.
this is what you would do for dynamic pages:
Code: Select all
<?php
$query = "test";
$language = "en";
echo "q=".$query."&hl=".$language;
?>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 13 times
Re: passing variable from php into flash, help please
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
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
Re: passing variable from php into flash, help please
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);
?>Re: passing variable from php into flash, help please
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.
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.
Re: passing variable from php into flash, help please
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.
Re: passing variable from php into flash, help please
Thank you,
I used the .fla file you post on this for me
I used the .fla file you post on this for me
Re: passing variable from php into flash, help please
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
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
Re: passing variable from php into flash, help please
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.
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.
Re: passing variable from php into flash, help please
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
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
Re: passing variable from php into flash, help please
if that is the case then just use the dummy variable like you suggested.