PDA

View Full Version : PHP and Flash : the introduction.



eyezberg
August 24th, 2003, 06:51 AM
as with(this){
eyezberg.postCount == 1000;
this.setValue("something special");
this.text = "Something Usefull on PHP";
this.text +="";

Ok, so this is post 1000 in season 2 of KirupaForums for me...
What I want to do on this occasion is start a thread like senoculars "best of" ones,
but not about Flash and /or Actionscript, but rather
about Flash and PHP usage; in the hope of providing
a usefull resource to refer to for commonly asked questions...

Please add only short comments to keep this as clean as possible,
or usefull tips'n'tricks of your own.

To start with: WHY should you bother learning PHP?

1/ it's free! most hosts give you acces in the basic packages.

2/ there's a huge community on the web with 1000s of scripts
for you to download and use:
- http://www.asp-php.net/scripts/asp-php/download.php
- http://phpclasses.goelette.net/browse.html/browse.html
- http://www.evilwalrus.com/
- http://codewalkers.com/archives/phpcoding/
- http://www.mattsscripts.co.uk/php_scripts.htm
- ... (just do a Google search...)

3/ it's very well documented, help is available on lots of
forums, and the scripting is very similar to actionscript.

4/ it lets you achieve things Flash alone can't:
-send server-side email (think "contact form, survey etc"...)
-read contents of your online folders (think "list all files/mp3s/images in a folder"...)
-store data in a database (think "guestbook"...)
-write data to a textfile in your webspace (think "guestbook - i got no database"...)
-upload (and resize!) images to your server (think "photogallery"...)
-create swf on the fly (MING library)
-maintain registered users records (think "login"...)
-...basically, save data to your server for later retrieval.
If you want a few examples of Flash/PHP integration, check out
- http://www.sephiroth.it
- http://www.flash-db.com
- http://www.phpforflash.com

Now, HOW do you use it?

The general usage is to combine Flash's LoadVars object
with a PHP script:
-you include the variable you want to send to PHP into the LoadVars
(myloadvars.variable1 = value;)
-you use myloadvars.sendAndLoad("yourphp.php", this, "POST") to access the script
-the script uses the recieved variables and sends the result back to Flash
via PHP's "echo" or "print" command as "&variablename=value" pairs.
-the onLoad handler of your LoadVars processes the results in Flash.

And WHAT are the tools you need?

Well, PHP is just text, so it can be coded in any text editor.
Macromedia's Dreamweaver provides convenient PHP highlighting,
same for Edit Plus ( http://www.editplus.com ) which I recommend
(cheap, lots of languages supported, and an add-on so you can test-compile
your actionscript code in Flash straight from the editor!)
, or have a look at PHPEd from http://www.nusphere.com ...

If you want to test on your computer, so as not to upload the scripts
every time you change something, I recommend using the Easy PHP package
from http://www.easyphp.org , I've been using this for some years now
on both Win 2k and Win XP Home without any problem, it's a one-click
install of the Apache server, PHP, phpMyAdmin to work on your databases..
All for free. If you want to try out another package, here's a link for
a huge collection:
http://www.hotscripts.com/PHP/Software_and_Servers/Installation_Kits/index.html
and that's all you need!
Don't forget to empty your browsers cache after making changes to your
swf or PHP files, so as to always preview the latest ones!

Conclusion: this is just an introduction, which shall be followed
by posts giving example code for the above mentionned features.
Check the sticky thread here for more resources in the meantime, and if
you search Google well, normally you'll always find a ready-to-use script
for all of your needs!


this.onRead = function(you){
thank[you];
doSmilie(grin);
}//end onRead
}//end with
//end post 1000

eyezberg
September 3rd, 2003, 03:35 AM
that's the kind of posts i did not want in here..well.. :)

ok, first follow up:

Why use LoadVars?

LoadVars is an object.
The advantages over good ole loadVariables are
- loadVariables would send ALL the variables in the clip it's called from to the script, so usage was to have a dedicated clip and to send the vars to that before calling loadVariables from there to avoid sending lots of junk; with LoadVars, you just go like this:
myLoadVars.variable = textfiled.text;
myLoadVars.otherVar = myCheckBox.getValue();
myLoadVars.stillAnotherVar = _global.variable;
etc
and all these and only these (yes i swear) get sent!

-LoadVars allows you to monitor load status with getBytesLoaded and getBytesTotal !!

-the recieving LoadVars object (which can be the same as the sending!) is also an object, so data is encapsulated in there and allows for easy retrieval using the this keyword.
(so be carefull: if you use "this" in a LoadVars, it won't refer to the clip the LoadVars object is in, but to the object itself!!

You'll have to be carefull with both loadVariables (if you still use it..) and LoadVars, coz they are asynchronous..what the F*** is that, you say?
That just means, Flash starts sending (and Loading), but does not stop code excution in order to wait for the result/answer from the server, so if you try to use the expected result variables before they have loaded, it will just fail!! You'll have to set up monitoring of the load status yourself, that's why the getBytesLoaded is so usefull!

The LoadVars object also comes with a "success" variable, which returns true if all went well and false if not, so you'd check on that before trying to use returned data. I'll post a full example including PHP next time (there's a few post here already, do a search..).

General run-down:
1.declare new LoadVars
2.populate the variables in it to be sent
3. set up the onLoad handler function (to process the returned data, including success check)
4.send or sendAndLoad either into the same loadVars or into a recieverLoadVars

If you want to check PHP's return data in your browser to debug, just use "send" only and "_blank" as target:
myLoadVars.send("script.php", "_blank", "POST");
and in your PHP, either echo or print the data as you would to Flash, a new browser window will open with whatever you'd send back to Flash.

Hope this helps, more PHP next time..

eyezberg
October 7th, 2003, 04:34 AM
2nd follow-up.

As you might have understood by now from the 2 previous posts,
Flash and PHP interact by way of sending variable/value pairs, separated by "&", back and forth (in its simplest form).

So loadVars.name=me
will generate in PHP
$name = "me";
and PHP: echo "&name=me";
will generate in Flash LoadVars.name = "me";
...quite simple.

I'll use the e-mail script from this thread
http://www.kirupaforum.com/forums/showthread.php?s=&threadid=33725
to explain a bit further (hope you don't mind..?).
So:

<?PHP
###################### Set up the following variables ######################
#
$to = "me@me.com"; #set address to send form to
$subject = "Results from your Request Info form"; #set the subject line
$headers = "From: Form Mailer"; #set the from address, or any other headers
$forward = 0; # redirect? 1 : yes || 0 : no
$location = "example.com"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################

## set up the time ##

$date = date ("l, F jS, Y");
$time = date ("h:i A");

## mail the message ##

$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";

if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}

mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}

?>


This script contains in the beginning 2 hardcoded variable/values:
$to = "me@me.com"; and $subject = "Results from your Request Info form";
then it loops through to other variables sent from Flash:

foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
(or with GET, depending on what's used..)
to add these to the msg variable;
"msg.= " is the PHP concatenation way, adds whatever is after the = to the variable.

Sooo..if for example you wanted to use different e-mail adresses to send the form results to, you wouldn't hardcode the $to mail adress in the PHP, but also send it from flash,
either by using different fla's with different mail adresses (hardcoded again, so not so good..),
or by setting the variable in the html source code of the flash/swf object by using the "flashvars" parameter:
Please read this Macromedia Technote (http://www.macromedia.com/support/flash/ts/documents/flashvars.htm) on how to use this feature, paying special attention to the URLENCODING !
Object Tag Example:
<PARAM NAME=FlashVars VALUE="to=youremailhere">
This variable will then be available as soon as the swf loads, and can be passed to PHP by inserting the value into the LoadVars as in : LoadVars.to = _root.to;

What all this means is that the above script provides a simple way of setting up a Flash/PHP e-mailer which will send any/all information to the adress you choose very easily without ever showing the user your e-mail (no spam).

This part
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
} lets you customize what the sender will see after submitting the form/mail.

You can "echo" any message back to Flash and display it in a dynamic textfield to let the user know the message was sent, just add "&status=" in front of your text,
as in "&status=Your message has been sent" :
LoadVars.onLoad =>assign returned var to the status_txt.text property by setting
status_txt.text=this.status; (where "this" refers to the LoadVars object you're sending the PHP result to, either the same one you used to send your variables, or a separate resultVars = new LoadVars).

Any question, post in another thread please,
if you want some very good sourcefiles for PHP/Flash interaction, check out
http://www.flash-db.com and
http://www.sephiroth.it/ (often down coz too much traffic, but VERY good files there, try a few times..)

'till next time, feedback welcome (too hard/too easy/details missing/feature requests...)

kevinhampton
January 10th, 2005, 02:03 PM
Could you provide more information on returning the status back to flash...

Here is my code, I need to return a status of success back to flash but I am unsure how to do that. Thanks.

<?
// Configuration of recipient and subject.
$recipient = "$f_mail";
$subject = "Check out mysite";
$mailheaders = "From: <$r_mail> \n";
$mailheaders .= "Reply-To: <$r_mail>\n\n";
$msg = "Check out this site, http://www.mysite.com. (http://www.mysite.com./) You'll love it.
Thanks::.. \n";
$msg .= "\n";
$msg .= "$yourmessage\n";
$msg .= "\n";
$msg .= "This message was generated at http://www.mysite.com (http://www.mysite.com/) by: $r_name\n";
$msg .= "..::peace & respect::..";
mail($recipient,
$subject,
$msg,
$mailheaders) or die ("Couldn't send mail!");
$recipient = "kevinhampton@yahoo.com ";
$subject = "My site";
$mailheaders = "From: <$r_name> \n";
$mailheaders .= "Reply-To: <$r_mail>\n\n";
$msg .= "This is who recieved it: $f_mail\n";
mail($recipient, $subject, $msg, $mailheaders) or die ("Couldn't send mail!");
?>

kikon
January 18th, 2005, 02:20 AM
Here's a question that crossed my mind number of times. Is it possible for separate flash movies (I'm talking about the one embeded in the html document...) to communicate between eachother using php or asp?

For example, picture this: What if your navigation was a flash movie; meaning that the links were animated with flash or what not.

Now, say somwhere else in a table on the page there's another flash movie. If I click a link in the first movie (the menu), let's say the scene changes in the other was well. Thus providing smooth animations between sections on your page while not having to design the whole page in flash making updating easy using a database since the rest of the page is html.

I wonder if this could be done?

kikon
January 18th, 2005, 02:56 AM
What I'm refering to can be accomplished with javascript, but I have yet to see it with php.

ex:

http://www.ashzfall.com/flash/fscomlib/frameplay.html

Yeldarb
January 23rd, 2005, 01:23 PM
Here's a question that crossed my mind number of times. Is it possible for separate flash movies (I'm talking about the one embeded in the html document...) to communicate between eachother using php or asp?

For example, picture this: What if your navigation was a flash movie; meaning that the links were animated with flash or what not.

edit: http://www.syntheticorange.com/showentry.php?id=3

Now, say somwhere else in a table on the page there's another flash movie. If I click a link in the first movie (the menu), let's say the scene changes in the other was well. Thus providing smooth animations between sections on your page while not having to design the whole page in flash making updating easy using a database since the rest of the page is html.

I wonder if this could be done?
Well, I don't think PHP is the optimal tool for that, there's something called a localConnection (i believe) that is used to communicate between two flash windows. If you look at Ahmed's site, he has an example where you draw in one window and it appears in another.

natronp
October 2nd, 2005, 10:15 PM
Is it possible to use flash remoting with PHP? it must be! anyone ever tried it? i've had good luck using remoting with ColdFusion and would love to call php pages through netConnection instead of using loadVars all the time!

matt_the_rat
April 24th, 2006, 10:01 AM
hey i got given this sql to make a database in mysql for a login tutorial and it comes up with an error saying you userid invalid default. CREATE TABLE auth ( userid int(4) unsigned zerofill DEFAULT '0000' NOT NULL auto_increment, username varchar(20), userpassword varchar(20), PRIMARY KEY (userid)); can someone help please. thanks