• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2617
  • Last Modified:

Calling a PHP function from Flash AS2

I have a problem calling a PHP function from Flash. Using the technique of triggering the function using a variable (see: http://www.tutorialized.com/view/tutorial/Calling-PHP-Functions-from-Flash/2333 ) does not work on my server, it throws a fatal error: "Fatal error: Function name must be a string in phpTest3.php on line 14". The final work will not rest on this server either and I need a stable script.

The PHP script must be in a function. If you can get any PHP function to trigger and print a variable from Flash on screen I should be able to do the rest myself. I trigger the AS that calls the PHP function from a button in the swf-file.

My test file: http://squaregrain.com/clients/ee/phpTest3.php

This is pressed for time.

Regards,
Andreas.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP Test</title>
</head>
 
<body>
<?php
	//$triggerVar = "flashFunction";
	function flashFunction() {
		echo("This is PHP!!!");
	}
	$triggerVar();
?>
</body>
</html>

Open in new window

0
iamcmag
Asked:
iamcmag
  • 5
  • 3
  • 3
  • +1
2 Solutions
 
Aneesh ChopraCommented:
Flash can't call a PHP function as you are expecting to achieve.

Although you should call the PHP with querystring parameters or POST variables.

and PHP should react based on received GET/POST variables and return the response to flash

you should use LoadVars.sendAndLoad() method.


here is a link which has few sample code on how to communicate with server-side scripting using Flash actionscript

http://www.flashadvisor.com/tutorial/ActionScript/LoadVars_Class_in_Flash-30-1.html
0
 
rascalpantsCommented:
You need to bridge the gap between Client-side scripting and Server-side scripting... and while Flash can do that in a Number of ways...  as Aneesh has mentioned above, it is not going to happen with just a call to the page...  

It seems like a backwards way to make something work, but just in case this is your only option... you can use ExternalInterface to call a JavaScript function... which then uses AJAX to communicated with your PHP scripts...

like I said though... this seems backwards, but there is a way to do it...

rp / ZA
0
 
iamcmagAuthor Commented:
Thx, guys. I have previously tried ExternalInterface.call to trigger a JS that loads the PHP page which then does its thing and auto closes, but this is not a pretty way to do it nor a good one since the parameters got fracked up while being passed back and forth.

I'll look into this and post back.

Do you now if it's possible to use LoadVars.sendAndLoad on another page (just resting on the server) than the Flash container? If it was possible to just send vars to another page that executes the script without displaying the page it wouldn't have to be in a function.

/A
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rascalpantsCommented:
yes, that is the purpose of the LoadVars... it is used to send data behind the scenes like AJAX...  techically... flash could do what AJAX does best... 5 years before the term was even coined.

;)

rp / ZA
0
 
CraybeCommented:
I have written code that should do what you want using sendAndLoad.  You can view it working at http://www.craybe.com.au/testflash.html

You can download the source files from:  http://www.craybe.com.au/ee-sendvar.zip

There isn't a lot to it so it should hopefully explain itself but if you need further information feel free to ask and I will comment the code.

Code is as follows:
//*************************************************//
//********************FLASH CODE*******************//
//*************************************************//
 
buttontest.onRelease = function(){
	sendvar = new LoadVars();
        //The Variable we will be sending to php to trigger the function
	sendvar.my_func_name = "flashFunction";
        //Call the php sending the variables of sendvar
	sendvar.sendAndLoad("phpTest3.php", sendvar, "POST" );
        //Run after php has been loaded
	sendvar.onLoad = function(success){
                //if php file is loaded correctly
		if(success){
                        //display the returned text for &returnedfromphp
			returnedtext.text = this.returnedfromphp;
		}else{
                        //display fail message
			returnedtext.text = "PHP failed";
		}
	}
}
 
//*************************************************//
//**********************PHP CODE*******************//
//*************************************************//
(Note you do not need the html header, body etc in the php file just the below code)
 
<?php
     //retrieve function name from flash
     $triggerVar = $_POST["my_func_name"];
     function flashFunction(){
              //return result to flash, you can return multiple vars
              //just seperate them with an &
              echo("&returnedfromphp=This is PHP!!!");
     }
     $triggerVar();
?>

Open in new window

0
 
rascalpantsCommented:
Since you are using AS 2.0, and you have the capabilities to use a class-based solution, I would recommend that.

It is poor programming practice to nest function inside of other functions...  so at the very least, you should seperate those out.

rp / ZA
0
 
CraybeCommented:
I agree with rascalpants, however as you are pressed for time what I have supplied will work.  If your working on a large project that will involve multiple calls to php to return data change the code, if it is just a single call and you aren't familiar or comfortable with a class-based solution use the code provided.  

Here is the updated flash code with the nested function separated:
var sendvar = new LoadVars();
 
sendvar.onLoad = function(success){
	if(success){
		returnedtext.text = this.returnedfromphp;
	}else{
		returnedtext.text = "PHP failed";
	}
}
 
buttontest.onRelease = function(){
	sendvar.my_func_name = "flashFunction";
	sendvar.sendAndLoad("phpTest3.php", sendvar, "POST" );
}

Open in new window

0
 
rascalpantsCommented:
that, or you, Craybe, could move the functions outside of the onRelease method for our Author... then he and you have a much cleaner solution.

;)

rp / Zone Advisor
0
 
CraybeCommented:
Isn't that what I did in the code snippet of my last post? ;-)  that or I'm misunderstanding you, which is possible, I'm working on a VB project and I haven't worked in AS2 or PHP for months hehe
0
 
rascalpantsCommented:
actually... no...  I was on EE mobile and didn't see the code snippet link... so I didn't even see that part of your post...  serves me right for posting a comment from my phone  ;)

rp / Zone Advisor
0
 
iamcmagAuthor Commented:
I've been tied up in another urgent project so I will dig into this again ASAP.

Thx for the posts so far.

/A
0
 
iamcmagAuthor Commented:
Sorry for the delayed response but I got caught up in another even more pressing project.

I have solved the problem and it was both my uncertainty of the LoadVars.sendAndLoad function as well as poorly written php code that was the problem (didn't know I had to use $_POST to get the vars). Rascalpants pointed me in the right direction by explaining that I could run the php code right off the server without having it as the Flash container, and Craybe provided a solution to triggering php inside a function which was how initially tackled the problem. So a split points solution seems fair.

Thx for the help guys :)

/A
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now