Solved

passing data / variables between flash and php - beginner - newbie - noob - n00b

Posted on 2004-09-14
7
5,553 Views
Last Modified: 2012-05-05
Please forgive the long-winded post; I wanted to be thorough and not waste anybody's time...

I have been googling for 2 days and I have no hair left.  I am fairly new to programming and love php.  I have done some really cool things with PHP/MySQL relatively easy.  I have been using Flash for a while but not much ActionScripting thus far.

I got this code from the tutorial I am following here:
http://www.kirupa.com/developer/actionscript/flash_php_mysql.htm

the utterly simple AS:

onClipEvent (load) {
  //assuming you have a personal web server and PHP installed locally
  loadVariables("test.php", this, "GET"); <--- note: I changed this from the original. <---
}

the utterly simple php:

<?php
//The value of $x would be printed to the screen but the SWF would not read the data
$x = "abc";
print $x;  <---SHOULD THIS BE REMOVED FROM THE SCRIPT ?

//The value of $x would be printed to the screen and because of the prefix 'myVar=', the SWF will //interpret this as being the intended value for the variable myVar in the SWF
print "myVar=$x";
?>

The second comment in the code above is a lie :)  Flash does not see it 'myVar='.

This tutorial seems to walk you through step by step and I couldn't think of anything easier to start with.  Yet, it does not work for me!  I have tried other tutorials out there (login/authentication) and end up in the same place... a flash text area (dynamic) with NO TEXT!

I think the PHP code is working but under some circumstances, the test.php pops up in a new window and prints "myVar=123" ...as in... <? echo ("myVar=123") ?> (when I modify the code that is)

Relative points...

1. Should my SWF be in the PHP page or in a seperate HTML file?  I have it in an HTML file (index.htm) but I've tried it in both the HTML file and the PHP file.

2. I have seen the code I am trying to use in several scripts <? print "myVar=$x"; ?> except EVERY time I have seen it, it looks like this <? print "&myVar=$x&"; ?> or atleast 1 of the added ampersands.

3. Another thing I tried was to add this parameter to the embeded flash object:
    <param name="allowScriptAccess" value="sameDomain" />

4. Would this extremely basic simple script vary for different versions of Flash/ActionScript?

5.  Should I be using the loadVariables or should it be loadVariablesNum? I've tried both...

6.  If I copy and paste this simple code you would think it WOULD work!  It does not!!!

I have been able to bring text into a Flash dynamic text area from a .txt file easy enough.  I understand the "&" is what Flash uses to seperate elements from the external text.

If I can figure out how to pass vars between Flash and PHP I am on to great things :)

No kidding, I have tried every combination between everything mentioned above.  I have looked everywhere I know of on the web.  I have not been able to find anything to get me started that is as simple as this code ( SHOULD BE )!

I'm desperate (no, really)! This is the first time I have had to post to find the answers I seek!

Please help, thanks in advance, and if I could offer more than 500 points I would!!!

Thanks again,

Kevin

P.S. Some good (Flash/PHP) n00b resources would be greatly appreciated.  Lot's of great PHP resources out there but not too much using the combination, for n00bs anyway :)
0
Comment
Question by:kevindolan
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
vickerleung earned 500 total points
ID: 12061311
Passing variables from PHP to Flash is quite a convenient stuff

The flow is like this:-

A SWF file: which is your movie, with a LoadVariables function, just like the one you have posted.
(But I gotta remind you that your script is within a clip event function, so this action script should be added inside a movieclip)

A PHP file: which will print out the variable, personally I prefer using echo
(e.g. <? echo ("&variable_name=" . $variable_name); ?>)
Reminder: The ampersand & is a must for Flash to know whats going on

If you really want to try the whole process, do the following:

Write a PHP file test.php like this one:
<?

$test = "abcdefg"
echo ("&test=" . $test);

?>

Open a new Flash file:
- Use the text tool, drag a text on the workspace
- change the text to dynamic text
- in the variable field, input "test"
- in the frame1, add the following actionscript

loadVariables("test.php", this, "GET");

Upload to the server in the same directory,
test it~ that should be ok~~!! Gd luck :)

P.S.

- Actually you can directly run the swf without embedding into a html file
- Actionscript 1.0 and 2.0 using the same script
- loadVaraiblesNum just specify the movie layer that the variables should pass to
(in this case, you are not necessary to use loadVariablesNum)
0
 

Expert Comment

by:pixelengine
ID: 12069910
OK, try this... in the EMBED and OBJECT tags add the following::::

<OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" WIDTH="151" HEIGHT="20">
              <PARAM NAME="movie" VALUE="yourswfhere.swf?x=<?echo $x ?>">
              <PARAM NAME="quality" VALUE="high">
              <EMBED SRC="yourswfhere.swf?x=<?echo $x ?>" QUALITY="high" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" TYPE="application/x-shockwave-flash" WIDTH="151" HEIGHT="20"></EMBED>
          </OBJECT>


Then once the .swf loads you will have x on the _root level in the movie ready for use.


cheers!

pixelEngine
0
 

Expert Comment

by:pixelengine
ID: 12069926
please note that I am an .asp guy and have no idea about php syntax so whatever I have for this here

?x=<?echo $x ?>">

in .asp would be

?x=<%=x %>">

so long as you get php to print the variable there you are rocking.

out.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:kevindolan
ID: 12070216
vickerleung
thank you so much, I greatly appreciate you also taking the time to address my entire post.  It worked but I can't resisit mentioning the scare I had.  I followed your instructions and pasted your code into my docs.  It didn't work! OMG I thought, what the hell is wrong with me :P  Then I ran just the test.php and got a parse error; looking into this error I noticed somebody mention "be sure to have your semi colons seperating each line of code".  Boom that was it, you/I overlooked a ";" after declaring the var.  Seriously, I was going to have a break down if I couldnt make your code work after everything I/you explained.

I can't believe I was unable to find anything on the web such as a tutorial this easy (and works) for somebody getting started using the 2 technologies together.
Would I be pushy to ask why you prefer using the echo function vs. print ?

Thanks again

I would also like to thank pixelengine and anybody else gracious enough to share their time and knowledge.  pixelengine I have not yet tried the instructions you posted but I certainly will first chance I get!  I will leave this post open for a couple more days so anyone who can add to it has the chance.  After my search on the web resulting in my post, I assume there are plenty of others in my position who will appreciate coming accross this post/solutions.
0
 
LVL 16

Expert Comment

by:vickerleung
ID: 12071636
kevindolan~

Haha

Sorry that I didn't test my code and release out to you :p

Actually I don't know why I prefer echo~ haha
But I have done a little research and hope this helps you ^^
http://www.faqts.com/knowledge_base/view.phtml/aid/1/fid/40

And I have put my source files in here~
http://www.geocities.com/vickerleung/test.zip

This one is a simple source for
Flash -> PHP -> Flash flow

For the Flash side, there is a input text field with variable property - input
This is the user input that will pass to the php through POST
And another text field with variable property - test
to receive the echo from the php script

The actiosncript this time is attached to the submit button as follows:

on (release)
{
      loadVariables ("test.php", _root, "POST");
}

And the php script is like the one below:

<?

      $test = $_POST["input"];
      echo ("&test=" . $test);

?>

Do enjoy playing with PHP and Flash~~!! :)
Hope these can help you
0
 

Author Comment

by:kevindolan
ID: 12072306
vickerleung,

excellent! all is well...

thanks for posting the answer to my next endeavor before I even had to think about it :)

Thanks again,

Kevin

pixelengine... I will stow this away for future reference as I'm sure I will find your method more usefull once I better understand what I'm trying to do.  It just seemed a little more complex than what I am able to work with yet.  However, with that said, what the heck do I know ;)

Thanks
0
 

Author Comment

by:kevindolan
ID: 12072330
pixelengine...

P.S. It's not what you posted that is complex, it's just that for what I have in mind and my understanding of things this early in the game; the php seems more straight-forward to me.

Thanks

Kevin
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Software Restriction Policy 8 865
Crash with shockwave flash 8 244
Where to download Acrobat flash offline installer? 2 242
How to block online streaming via TMG2010 ? 2 132
This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question