
|
View Full Version : send php variables to flash
madpato 12-17-2009, 10:23 AM Hello
I've been trying to create a small flash swf movie that only displays some variables located in a php file which grabs some lines of a remote site, well, i don't have flash experience, but looking at the examples in the web, it's not too hard, but i'm always missing something.
Here is my php code:
<?php
$url = "http://url-that-has-the-data";
$line = file($url);
$var1=$line[264];
$var2=$line[270];
$var3=$line[276];
$var4=$line[283];
echo $var1;
echo $var2;
echo $var3;
echo $var4;
?>
Just that, well i want to send those vars to diferent fields inside a flash movie, i'm sure that someone with flash experience will find it easy. I don't even need a button to do the job, just display that in flash.
I've followed an example to send the vars to just one textbox (dynamic), but i want to send it to diferent textboxes, I have this example but just for one, and it does not work.
stop();
path = "http://location-to-my-php";
values = new LoadVars();
values.onLoad = function (success) {
if(success){
output.text = values.returnVal;
}else{
output.text = "fail";
}
}
I hope someone can point me in the right direction, thanks.
roarindustries 12-18-2009, 07:05 AM var uri:String = "/path/to/script.php"
var lv:LoadVars = new LoadVars();
lv.onLoad = function(success:Boolean) {
if (success) {
// 'this' keyword refers to LoadVars object
// if you want to target textfields
// make sure they are in scope, i.e.
// this._parent.myText
// or
// _root.myMovieClip.myText
// etc.
// foo & bar being your variable names...
trace(this.foo);
trace(this.bar);
}
};
lv.load(uri);
As far as the php goes, Flash expects the data to be returned as name-value pairs separated with '&'.
It's also a good idea to both pre & post fix with an '&' too.
Build your output string and echo once i.e.
$output = "";
$output .= "&varName0=value0";
$output .= "&varName1=value1";
$output .= "&varName2=value2";
$output .= "&";
echo $output;
aniketh 12-18-2009, 07:22 AM Also, alongwith previous solution, don't forget to urlencode the values
madpato 12-18-2009, 11:49 AM thanks will give it a try :)
madpato 12-18-2009, 01:09 PM I've been trying but i can't work it out this is what i have:
In flash:
var uri:String = "http://url-to-myscript.php"
var lv:LoadVars = new LoadVars();
lv.onLoad = function(success:Boolean) {
if (success) {
// 'this' keyword refers to LoadVars object
// if you want to target textfields
// make sure they are in scope, i.e.
// this._parent.myText
// or
// _root.myMovieClip.myText
// etc.
// foo & bar being your variable names...
trace(this._parent.var1);
trace(this._parent.var2);
}
};
lv.load(uri);
I do have a couple of dynamic textboxes each one called var1 and var2 that should receive the vars from the php which are also called var1 and var2.
Php code:
<?php
$url = "http://url-to-some-site";
$lineas = file($url);
$output = "";
$output .= "&var1=$lineas[264]";
$output .= "&var2=$lineas[276]";
$output .= "&";
echo $output;
?>
hope you can help me, thank you.
roarindustries 12-18-2009, 01:42 PM The traces were just for example m8, all yours would do would be trace empty textfields? Also, try calling your php script directly, is it giving you the correct output?
Actionscript would be something like:
this._parent.var1.text = this.var1;
Can you post your fla? Please tell me what version of Flash you're using if you do!
madpato 12-18-2009, 01:55 PM the php script gives me the correct output:
&var1= 20.986,95 &var2= 500,56
And yes, is just to put a variable value to an empty textfield.
madpato 12-18-2009, 02:26 PM :sigh:
trying this:
trace(this.parent.var1.text = this.var1);
I got this output in one of the textfields:
_level0.var1
thanks for your time.
roarindustries 12-18-2009, 02:37 PM ...try that without wrapping it in a trace
Just post your fla if you can! And Flash version helps, can't post an example unless I know what version you can open.
madpato 12-18-2009, 02:56 PM I'm working with cs4 here are the files: http://www.mediafire.com/?zjinzd0tdwf
thanks.
roarindustries 12-18-2009, 03:03 PM No problem.
Right can't post links so please extrapolate...
nowplaywithme.com/code/actionscript/2/loadvars/loadvars.000.html
Source files:
nowplaywithme.com/code/actionscript/2/loadvars/loadvars.000.fla
nowplaywithme.com/code/actionscript/2/loadvars/echo.phps
roarindustries 12-21-2009, 07:40 PM Ok, the problem isn't with your actionscript or even the output of
php, it's with the way you are obtaining or _scraping_ your data, your
approach is pretty naive.
Scraping is fragile at the best of times.
Here's a couple of links for you to check out:
http://www.bradino.com/php/screen-scraping/
http://www.thefutureoftheweb.com/blog/web-scrape-with-php-tutorial
Just google "php html scraping" for more.
A quick and dirty solution:
<?php
$uri = "http://si2.bcentral.cl/Basededatoseconomicos/951_portada.asp?idioma=E";
$lineas = file($uri);
$uf = trim( strip_tags( $lineas[264] ) );
$dolar = trim( strip_tags( $lineas[276] ) );
$datos = "";
$datos .= "&uf=$uf";
$datos .= "&dolar=$dolar";
$datos .= "&";
$encoded = urlencode($datos);
echo $datos;
// returns &uf=20.976,77&dolar=502,59&
?>
madpato 12-22-2009, 12:23 PM thanks a lot, will try it
|