PHP and Flash : the introduction

[AS]
as with(this){
eyezberg.postCount == 1000;
this.setValue(“something special”);
this.text = “Something Usefull on PHP”;
this.text +="";
[/AS]
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:

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

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!

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

that’s the kind of posts i did not want in here…well… :slight_smile:

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…

[FONT=Courier New]hey i got given this sql to make a database in mysql for [/FONT][FONT=Courier New]a login tutorial and it comes up with an error saying you [/FONT][FONT=Courier New]userid invalid default.[/FONT][FONT=Courier New][/FONT] [FONT=Courier New]CREATE TABLE auth ( userid int(4) unsigned zerofill DEFAULT ‘0000’ NOT NULL auto_increment, username varchar(20), userpassword varchar(20), PRIMARY KEY (userid));[/FONT] can someone help please. thanks[FONT=Arial][/FONT]