Flash Kid Logo  
Home - Tutorials - Applications - Games - Books - Directory - Community - Site Map - Contact Us  
 
 


 
 
Flash Contact Form Tutorial - 1 / 2

Author: Salman Awan
Difficulty Level: Advanced
Flash Version: Flash MX 2004
Sources to Download: flash_contact_form.zip
Prerequisites: Input Text field, Button, ActionScript, Basic PHP knowledge

Introduction:

Setting up a flash contact form is important for any business’s flash website, to enable their customers to send queries and orders. In this tutorial we will learn how to create a flash email form using PHP. We can use same tutorial for ASP page instead of PHP. ASP Section in the end will help you do this quickly.


Below is demo of flash mail form that we will create in this tutorial (You won’t be able to send or receive an email in this Demo since it is not configured for your email address).

Demo 1 - Simple Flash php Contact form

Setting up Timeline:
Open a blank flash document and create two layers. Select top layer’s first frame and enter this line of code in ActionScript panel.

serverFile = "mail.php";

It creates a variable, that holds php file name which will be used to send email. After these steps Timeline should look like this screenshot.

Timeline after adding serverFile variable
Fig. 1 - Timeline after adding serverFile variable

Setting up Stage:
Now we need to create flash email form’s layout. Select second layer’s first frame. And create three Static text fields, that will serve as labels. Enter text in this order, from top to bottom, in these three fields, “From Name:”, “From Email:” and “Message:”

Also add three more text fields in front of three labels added above, make them Input Text Fields. Also enable “Show border around text” for all three input text fields. Name these three input fields from top to bottom as, from, email and message.

Since only Message field might have multiple lines, so we’ll have to make it a Multiline input field. Red boxes in screenshot below will guide you for these steps.

How to create Input text field, enabling show border and making multiline field
Fig. 2- How to create Input text field, enabling show border and making multiline field

Add a Dynamic text field that will receive and display any error messages during sending email process. Add it anywhere you like your messages to be displayed. It should be Multiline but without borders. Name this field as status.

We also need to enter two Button symbols, one for clearing the form, and another one for sending email. Label these two buttons as Clear & Submit respectively.

After above setup, flash email form on stage should look like this screenshot.

Stage after adding Labels, Input Fields & Buttons
Fig. 3- Stage after adding Labels, Input Fields & Buttons

Adding ActionScripts:
Now we will start adding real magic, starting with Clear button. Click on Clear button to make sure ActionScript panel displays actions for this button. Add below code to ActionScript panel while Clear button is selected.

on (release) {

from.text = "";
email.text = "";
message.text = "";
status.text = "";

}

This will simply add an event to clear button, that whenever it is released, it will clear all input & status fields.

Before we add code to flash email button labeled as Submit, we will write a function in order to separate form validation logic from submit logic. Checks like valid email format & blank fields can be added into this function.

Click first frame of top layer in Timeline, and then add below code under previously added single line of code.

function formValidationChecks(){

if ((!email.text.length) || (email.text.indexOf("@") == -1) || (email.text.indexOf(".") == -1)) {
status.text = "Please enter a valid E-mail address";
return false;
} else if (!from.text.length) {
status.text = "Please enter Your Name";
return false;
} else if (!message.text.length) {
status.text = "Please Enter Your Message";
return false;
}
return true;
}

Now we can call this function in Submit button’s on release event, to make sure form input passes all checks before email is sent.

Select Submit button to make sure ActionScript panel displays proper window. Add below code to ActionScript panel while Submit button is selected.

on (release) {
            status.text = "";
           
            if(formValidationChecks()){
                       
                        //filling LoadVars object with variable values
                        my_lv = new LoadVars();
                        result_lv = new LoadVars();
                       
                        my_lv.from = from.text;
                        my_lv.email = email.text;
                        my_lv.message = message.text;
                       
                        trace( my_lv.from + " " + my_lv.email + " " + my_lv.message);
                       
                        my_lv.sendAndLoad(serverFile, result_lv, "POST");
                       
                        // When the results are received from the server...
                        result_lv.onLoad = function(success:Boolean) {
                                    if (success) {
                                                from.text = email.text = message.text = "";
                                     }
                                    status.text = result_lv ["serverResponse"];
                        };
            }
}

Tip: Detailed comments on what these lines are doing can be found in downloadable source code for this tutorial.

We are now good to give it a test. Email will not be sent as we have not created and placed a PHP file yet, however we can test basic functionality of form and ensure our inputs are ready to be passed to PHP. Trace function in above code will display our entered inputs. Control > Test Movie ( Ctrl + Enter) to give it a test run.

Creating PHP Page:
Once Flash part is done, now we need to create a blank web page and name it mail.php, add following code in it.

<?php

$from    = $_REQUEST["from"];
$email   = $_REQUEST["email"];
$message = $_REQUEST["message"];

$to          = "flashkid@flashkid.org";
$subject  = "Posted from FlashKid.org";
$full_msg = "From Name: " . $from . "\n From Email: " .  $email .  "\n Message: " . $message;

$ok= mail($to, $subject, $full_msg);

            if($ok) {
                        echo( "&serverResponse=The mail was successfully sent");
            } else {
                        echo("&serverResponse=Sorry but the email could not be sent. Please try again!");
            }

?>


Tip: This page won’t be displayed, and variables will be sent in background using POST method, thus no need to work on its looks.

This PHP file will receive variables sent from flash, and compose a simple email using them. Email will be sent using mail method of PHP. You just need to change the $to variable’s value with your own email where you want that email to be received.

Tip: Sometimes emails sent through servers go into your Junk Folder in mail box, do check that if you are not receiving them in Inbox.

Note: For sending an email, we will need an email Server. If you have an hosting package, it will most probably include one already, you just need to place your flash SWF & PHP file on Web Server in same directory, and it will be good to go.

Using ASP Page:
if your server supports ASP rather than PHP, we can use an ASP page for sending emails, otherwise skip this section. Create a blank page with name mail.asp and enter following code in it.

<% @language="VBSCRIPT" %>
<%

Dim myMail, myBody

myBody = "Name: "& request.form("from") & vbcrlf
myBody = myBody & "Email: "& request.form("email") & vbcrlf
myBody = myBody & "Message: "& vbcrlf & request.form("message")

Set myMail = CreateObject("CDONTS.NewMail")

myMail.BodyFormat=1
myMail.MailFormat=1
myMail.From=request.form("from")
myMail.To="flashkid@flashkid.org"
myMail.Subject="Flash MX Basic Email"
myMail.Body=myBody
myMail.Send

set myMail=nothing

%>


We might need to tell Flash, which file to use. In first frame of top layer, replace

serverFile = "mail.php";

with

serverFile = "mail.asp";

Publish and upload HTML, SWF & ASP/PHP files on your web server in same directory. Now you can receive emails at address mentioned inside ASP/PHP file.


Troubleshooting Flash Contact Form:
If you are facing any issues or problems implementing your flash contact form, please go through second tutorial in flash contact form series here.


Exercises:

  1. Why this demo does not work on your local system to send email? What is your local system missing to make it work?

  2. In PHP file, what is the difference between $from and $_REQUEST[“from”] ?

  3. Identify the line of Code in flash that sends variables to PHP/ASP file, and line of code in external file that send results back to Flash.

  4. Customize flash file to take sender’s email as input too, and sending it in email.

  5. Customize flash file so that upon successful submission of email, movie goes to and stop on second blank frame  with success message.

 
 

 
 
Related Links
Discussion Forums for Tutorials
 
 

Feedbacks :
May 26, 2008 - Tam : wow! thanks for this great contact page tutorial. Really easy to follow and works a treat! thanks.
Mar 31, 2008 - Robb Albright : I downloaded a flash website template, which I am now in the process of attempting to customize to my own needs. I have copied your contact form in template, and

uploaded both files (flash.swf and mail.php) to server in the flash folder like this:

http://www.mycooldomain.com/flash/mail.php
http://www.mycooldomain.com/flash/flash.swf

but i am not able to receive emails? what i am doing wrong here?

Mar 31, 2008 - Salman Awan : @Rob please upload your mail.php file in root folder where index.html page is placed, rather than in 'flash' folder.

Once an SWF file is loaded in an HTML page, default directory for that swf changes to directory of that HTML page. Since script in flash looks for "mail.php" rather than "flash/mail.php" that's why it is not able to find it once loaded in index.html file.
 
 
 
Send Feedback
Feedback Form
 
 


 
 
 
 
© 2010 flashkid.org! All Rights Reserved.
 
   
 
a project of: salmanawan, other projects: xonsolutions.com