PDA

View Full Version : Load PHP on page refresh



golden14
March 17th, 2008, 09:56 PM
I've been working on saving variables to an SQL database (through PHP) and then passing them back into Flash, and its working exactly the way it should at the moment. It's set up so that when you click a "save" button, it saves the vars and then reloads them right away.

I need to set it up so in addition to doing this, it will load all the data when the page refreshes (essentially, the second part of the code show below). I'm just not sure what to change and/or add to make it work. Any help would be great - thanks!!!

ActionScript Code:

function saveIt(e:MouseEvent):void
{
var request:URLRequest = new URLRequest ("http://localhost/test.php");
request.method = URLRequestMethod.POST;

var variables:URLVariables = new URLVariables();

variables.tName1 = test1.text;
variables.tName2 = test2.text;
variables.tName3 = test3.text;

request.data = variables;

var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);
}


function onComplete (event:Event):void
{

var variables:URLVariables = new URLVariables( event.target.data );

test1.text = variables.tName1;
test2.text = variables.tName2;
test3.text = variables.tName3;
}

xavii
March 17th, 2008, 10:42 PM
so you want it to automatically load the data when the page (as in the page that your flash obj is embedded in) loads (refreshes)?

golden14
March 17th, 2008, 11:24 PM
Yeah, exactly. Any ideas or suggestions?

xavii
March 17th, 2008, 11:41 PM
Yeah, exactly. Any ideas or suggestions?

your best bet is to do exactly what you are doing at the moment.just throw a call to your function in a frame and that function will be executed when the flash comp starts up

just make sure that your php file will output data from your data source without receiving any new stuff to insert first.



// just whack this into a frame and it will call the function on startup to load your variables.
loadVariables();


function loadVariables(){
var request:URLRequest = new URLRequest ("http://localhost/test.php");
request.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader (request);
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.load(request);
}

golden14
March 17th, 2008, 11:46 PM
The PHP file I'm currently using is both saving the data to SQL and also echoing it back to AS3. So are you saying maybe I should have 2 PHP files? One for saving and then another for loading?

Thanks for the help!