|
||||||
|
||||||
Flash Tutorial - Flash & PHP Forms
304 Interaction
Flash Compatibility: MX
2004
(Principles and ActionScript are the same in Flash MX & Flash 5)
Written by: Phil Schulz
Length: 2400 words
Assumed Knowledge: Familiarity with Flash and uploading files to a web server would be useful.
Level: Medium to Hard
Access: Members Bonus Tutorial. Full text
Feedback forms are an essential part of any site. They are quite straight forward in Flash except that Flash needs a little support from a scripting language.
The scripting language actually emails the from's content. Flash cannot do this. In this instance I have selected to use PHP as the scripting language. There are quite a few other scripting languages that you could use. I have selected PHP because it is a free and is also the most widely used scripting language on the Net.
Note: Scripting languages such as PHP are Server Side. That is your web site controls the script. Flash is not Server Side but Client Side. This means that Flash Movies are controlled by the users Browser or the Clients computer. You do not know the set up of the local computer which is why Flash cannot send an email form. Flash can only collect the information. The PHP (or other scripting language) then sends the form info.
Important Note: For this Form to work you will need to have a PHP Enabled Web Server. Ask your web provider to supply this. Almost all web host companies will supply this but some may charge a small fee.
Cross Ref: For more info on using PHP see: Intro to PHP and Flash
Example:
Download the Flash file
IntAct 304a (5 Zipped files)
Example of a Flash - PHP form.
Try the following in the Form above:
Step one: Setting up the Document
Step two: Setting up the Input Boxes
Input boxes are the areas that the user types into. In HTML they are often called Fields. Whatever you call them they amount to the same thing: An area that the user can type into like this:




| nameField | phoneField | |
| emailField | mobileField | |
| companyField | faxField |


| addressField | messageField |
Step three: Creating the Form Text and Buttons

Note: If you fail to do this any text that you subsequently type will be set as an Input Box. If you fail to reset the Property Inspector back to Static Text anything you type can always be reset via the Property Inspector at a latter time.


Note: If you do not wish to create your own buttons simply pull two buttons out of the Common Library and type a label next to them. To open the Button Library go to: Window > Other Panels > Common Libraries > Buttons
Cross Ref: To learn more about creating your own buttons see the tutorial on: Buttons
Your Stage is now got all the standard items that you will find on any form.
Step four: The Error Box
The Error box is the bit that you hope that no-one every sees! If some one does make an error it will look like this:

The Error dialog Box.
At a latter stage it will be necessary for us to name the Error Box so that it maybe referred too in the ActionScript. In order to be able to do this the box needs to be a Symbol.



on (release) {
this._x = 2000;
}
This ActionScript moves the Error Box off Stage so that it is not visible. In this case to x position 2000. The Error Box is now finished and is ready to be placed on the Main Stage.


Step five: The ActionScript in frame 1
// These names must be the
same as the dynamic input fields & must be on a single line
in the Actions box:
fieldname = new Array(0, "nameField", "companyField", "addressField", "emailField", "phoneField", "mobileField", "faxField", "messageField");
// This must be in the same order except
with a question mark? like this:
awop = new Array(0, "nameField?", "companyField?", "addressField?", "emailField?", "phoneField?", "mobileField?", "faxField?", "messageField?");
Step six: The Reset ActionScript
Step seven: The Submit ActionScript
Note: In the ActionScript above to type the line: | use: Shift + Back Slash \
Step eight: Frame 2
In frame two of the Movie it is possible to give the user feed back so that they know that the form has been sent correctly.



Step nine:Saving and Exporting the Flash Movie
The Flash Movie is now Finished.
The only thing remaining to do is to publish the Flash Movie, create the PHP
and test that it all works.

Step ten: PHP Document 1 - global.inc.php
There are two PHP documents that you will need. This first one is basically a default document that is the same irrespective of the form.
function pt_register()
{
$num_args = func_num_args();
$vars = array();
if ($num_args >= 2) {
$method = strtoupper(func_get_arg(0));
if (($method != 'SESSION') && ($method != 'GET') && ($method
!= 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method
!= 'ENV')) {
die('The first argument of pt_register must be one of the following: GET, POST,
SESSION, SERVER, COOKIE, or ENV');
}
$varname = "HTTP_{$method}_VARS";
global ${$varname};
for ($i = 1; $i < $num_args; $i++) {
$parameter = func_get_arg($i);
if (isset(${$varname}[$parameter])) {
global $$parameter;
$$parameter = ${$varname}[$parameter];
}
}
} else {
die('You must specify at least two arguments');
}
}
?>
Step eleven: PHP Document 2 - process.php
// Names below must match the names used used for your input boxes (fields) in Flash
include("global.inc.php");
$errors=0;
$error="The following errors occurred while processing your form input.<ul>";
pt_register('POST','nameField');
pt_register('POST','emailField');
pt_register('POST','companyField');
pt_register('POST','addressField');
pt_register('POST','phoneField');
pt_register('POST','mobileField');
pt_register('POST','faxField');
pt_register('POST','messageField');
if($errors==1) echo $error;
else{
$where_form_is="http".($HTTP_SERVER_VARS["HTTPS"]=="on"?"s":"")."://".$SERVER_NAME.strrev(strstr(strrev($PHP_SELF),"/"));
// Useful if you need today's date:
//$today = Time();
//$Date = date("y-m-d");
// Names below must match the names used used for your input boxes (fields)
in Flash
$message="
Enquiry Form:
Name: ".$nameField."
Email: ".$emailField."
Company: ".$companyField."
Address: ".$addressField."
Phone: ".$phoneField."
Mobile: ".$mobileField."
Fax: ".$faxField."
Message: ".$messageField."
";
$message = stripslashes($message);
// Fill in the details below for where you want the email to go to
mail("my@email.com","Website
Form Reply ",$message,"From: www.myWebSite.com");
}
?>
Step twelve: Testing the Form
Just to remind you this form will not work unless your web server is PHP enabled. For the same reason you cannot test this form from your own computer (unless you are running a PHP web server - which is not likely). So you will need to upload the following files to you web server:
You should then be able to navigate to the appropriate web page using you Internet Explorer or an alternative Browser and test the form.
Hopefully it should work perfectly first time !!
Why not try out webwasp's new community. Meet new people, find friends in your area: Webwasp Mates & Dates
|
|
|
All material on this site is protected under international copyright © law. DO NOT reproduce any material from this site without written permission. Please ask as permission is often granted.
|