how do I call a swf file from php, and pass variables to the flash file

crescue
crescue used Ask the Experts™
on
Hi experts,
How do I call a swf file from a php form, and pass variables to the swf form ?
Lets assume the swf form is called mydata.swf
my php form is myphp.php, and I try to pass the variables USERNAME, NAME, and STATUS to my flash file.
I don't need to read back from flash, but I want to do something with the variables that I receive from PHP.
I'm using Flash CS3, actionscript2, and flash 5.3

Thanx for ur help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Engineering Manager / Architect
Commented:
You can use swfObject for that...

http://blog.deconcept.com/swfobject/ (this is an old version... that I still use to date :P)

<code>

I can elaborate further if you wish

-V


<HTML>
<HEAD>
<TITLE>My Webpage</TITLE>
<script type="text/javascript" src="./swfobject.js"></script>
</HEAD>
<BODY>
<div id="flashcontent">
</div>
<script type="text/javascript">
      // <![CDATA[
      var so = new SWFObject("mydata.swf", "Movie", "800", "600", "8", "#FFFFFF");
      so.addVariable('USERNAME','<?php echo($username); ?>');
      so.addVariable('NAME','<?php echo($name); ?>');
      so.addVariable('STATUS','<?php echo($status); ?>');
      so.write("flashcontent");
      // ]]>
</script>
</BODY>
</HTML>

Then in your ActionScript 2 code you'd use those values like this:

var _username:String = _root.USERNAME;
var _name:String = _root.NAME;
var _status:String = _root.STATUS;

Open in new window

Author

Commented:
please
Thanx for ur prompt response and help

Author

Commented:
Hi I went to the link u provided, and it sounds great !!!
By the way, do you know how can I use a button from flash embedded in PHP, and act as a regular PHP button, but when pressed performs the function(s) in PHP and FLASH at the same time.
Probably this tool makes it !!! :)

Thanx
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Antonio EstradaEngineering Manager / Architect

Commented:
Well, not quite...

php is a server-side language, meaning that before the page gets delivered to the user, it's rendered by the server. Flash on the other hand is client-side, meaning it works just based on what the user does.

So, you can't really access functions defined in a php script, but you can do a workaround using technologies like AJAX.

Let's say you have a database that holds user accounts, you select one and it's sent to Flash using swfobject. Then, inside the Flash file, you give the user the opportunity to change that name, and you want to save that change in the database. Since the database link is closed 'cause you already retrieved the data from the server, you can't save it by just calling a php function (that no longer exists).

So, you call a JavaScript (which is client side too) function from Flash sending the new username value. Using AJAX you can call another php script that just saves the data into the database.

I can provide a full solution (with code and everything) if you'd like.

-V

Commented:
Sounds like he just want to have a fancy Flash button nothing more nothing less...
Maybe he could create a button in Flash without any ActionScript attached to it so that it responds to the button rollOver/rollOut/Click... and lay a div layer on top of the embedded SWF button that gets click event to fire whatever action he'd like to run when it is clicked???

Just a thought...

CyanBlue
Antonio EstradaEngineering Manager / Architect

Commented:
Hah, guess you're right CyanBlue... at this time of the day I'm used to make things more complex than they are...

Anyway, the first post should solve the problem at hand (getting values in Flash from php), making a button do something could be really easy with some JavaScript and a getURL(); function.

Quick example:

<code>

-V
//JavaScript
function doSubmit() {
  var o = document.getElementById('myform');
  o.submit();
}

//Flash
myButton.onRelease = function() {
  getURL("javascript:doSubmit();");
}

Open in new window

Author

Commented:
Great Vulturous,
May you elaborate further the first and second example please.
On the flash button, yes it was the solution I was looking for, and I have tried several steps with no success :(
In the quick example that you provided, it just animates the flash button, lets say, right ?, and by JavaScript 'myForm', are u referring to the name of the form or the ID tag  in the form.
Thanx a lot for ur help, and the clarification from CyanBlue !

Author

Commented:
Great expert ! Thanx 4 ur help !
Antonio EstradaEngineering Manager / Architect

Commented:
Hey there.

Sorry it took me so long to answer, but here it is... the full project available for download, it's a php file that sends some values to Flash, and that same Flash changes the value of a DIV element without reloading the page. It can be used for a lot of things like submitting a form, going to some other webpage, etc.

Link: http://vulturous.110mb.com/eefiles/phpFlash.zip

Code:

Hope this helps :)

-V
//HTML+php+JavaScript

<?php
  $username = "JohnDoe44";
  $name = "John Doe";
  $status = "Active";
?>
<HTML>
<HEAD>
<TITLE>My Webpage</TITLE>
<script type="text/javascript" src="./swfobject.js"></script>
<script language="JavaScript">
function changeDiv(div,text) {
  document.getElementById(div).innerHTML = text;
}
</script>
</HEAD>
<BODY>
<div id="flashcontent">
</div>
<script type="text/javascript">
      // <![CDATA[
      var so = new SWFObject("myData.swf", "Movie", "800", "600", "8", "#FFFFFF");
      so.addVariable('USERNAME','<?php echo($username); ?>');
      so.addVariable('NAME','<?php echo($name); ?>');
      so.addVariable('STATUS','<?php echo($status); ?>');
      so.write("flashcontent");
      // ]]>
</script>
<div id="testDiv">This text will change when Flash's button is pressed</div>
</BODY>
</HTML>

//Flash

txtUsername.text = _root.USERNAME;
txtName.text = _root.NAME;
txtStatus.text = _root.STATUS;

changeTxt.onRelease = function() {
	getURL("javascript:changeDiv('testDiv','"+txtToChange.text+"');");
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial