PDA

View Full Version : Passing variables from PHP to Flash


peter5
12-04-2005, 09:48 PM
Hi all, I have been googling and searching the forums for hours, and have not come up with anything that tells me concisely what I should do.

Below is my PHP code, I have no Flash code yet as I cannot figure out what to do. I think it has something to do with loadVars(), however the macromedia online documentation is unhelpful, and all the forums seem to be about passing data to php, I am wanting to get it from php.

I want to return 13 variables from my php code, and then use them in my flash code. Any help,code snippets, or tutorial pointers would be extremely welcome.

<?
//Open Databaseconnection
$objconn = mysql_connect($mySQLserver, $mySQLUID, $mySQLpass);
if (!$objconn)
{
die('Could not connect: ' . mysql_error());
}

$objconnDB = mysql_select_db($mySQLDatabase,$objconn);
if(!$objconnDB)
{
die('Could not open' . $mySQLDatabase . mysql_error());
}

$level = "1";

$row_query = mysql_query(" SELECT * FROM `quiz` WHERE level=".$level." ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_array($row_query);
//break the row into the various variables
$tQuestion = $row["question"];
$tAnswerA = $row["answerA"];
$tAnswerB = $row["answerB"];
$tAnswerC = $row["answerC"];
$tAnswerD = $row["answerD"];
$tCorrect = $row["correct"];
$tAudienceA = $row["audienceA"];
$tAudienceB = $row["audienceB"];
$tAudienceC = $row["audienceC"];
$tAudienceD = $row["audienceD"];
$tFriendname = $row["friend_name"];
$tFriend1 = $row["friend1"];
$tFriend2 = $row["friend2"];

//Close Database
mysql_close($objconn);

print "question=".$tQuestion;

?>

Cota
12-04-2005, 10:56 PM
Here is an ASP example
http://www.actionscript.org/forums/showthread.php3?t=67379
The Actionscript will be the same, but you'll be using
echo "&VarName="
you get the idea..

peter5
12-05-2005, 01:24 AM
Okay so my code so far::

submit.onPress = function(){
newQuestion = new LoadVars();
newQuestion.onLoad = function(success)
{
if(success)
{
tQuestion = this.tQuestion;
tAnswerA = this.tAnswerA;
}
}
myVars.load("quiz.php");

myInput.text =tQuestion;
}

stop();


...
echo "&tQuestion=".$tQuestion;
echo "&tAnswerA=".$tAnswerA;
echo "&tAnswerB=".$tAnswerB;
echo "&tAnswerC=".$tAnswerC;
echo "&tAnswerD=".$tAnswerD;
echo "&tCorrect=".$tCorrect;
echo "&tAudienceA=".$tAudienceA;
echo "&tAudienceB=".$tAudienceB;
echo "&tAudienceC=".$tAudienceC;
echo "&tAudienceD=".$tAudienceD;
echo "&tFriendname=".$tFriendname;
echo "&tFriend1=".$tFriend1;
echo "&tFriend2=".$tFriend2;
?>

However when I run the example, the text shows up as "undefined". Any ideas why this is?

Cota
12-05-2005, 01:44 AM
newQuestion = new LoadVars();
newQuestion.onLoad = function(success)
{
if(success)
{
tQuestion = this.tQuestion;
tAnswerA = this.tAnswerA;
}
}
submit.onPress = function(){
myVars.load("quiz.php");
myInput.text =tQuestion;
}
stop();

peter5
12-05-2005, 02:48 AM
Thanks for your help, I like the way your code is set out much better.

I also made a mistake in my variable names
myVars.load("...")
instead of
newQuestions.load("...")

The code above makes the displayed variable undefined as the line:
myInput.text =tQuestion;
appears to begin before:
myVars.load("quiz.php"); (should be newQuestion.load("quiz.php"))
has finished running.

I solved this by placing the line inside the onload function as follows:

newQuestion = new LoadVars();
newQuestion.onLoad = function(success)
{
if(success)
{
tQuestion = this.tQuestion;
}
myInput.text =tQuestion;
}

submit.onPress = function()
{
newQuestion.load("quiz.php");
}
stop();


Thanks for your help COTA. I posted the above just so that anybody who searches/googles this post will have a clear answer. I will also post my entire code snippet when I am done (for the benefit of others).

Cota
12-05-2005, 04:38 AM
I didnt notice the myVars.load, easy miss though...good you solved it.