Creating a Contact Form in Flash Using AS3By Blue_Chi | Flash CS4 | ActionScript 3.0 | Intermediate | Page 1, 2
This tutorial will teach you how to create a simple contact form which the users of your website can use to send you message directly from the website. The script will be written in ActionScript 3.0 and will use a PHP script to send the message through to your email inbox.
This tutorial does not require you to know PHP as the script is simple and can be implemented without modifications. In order to understand the AS3 script would have to know the basics of variables, event handling, and the basics of the URLLoader Class and the URLVariable Class. You may refer to our previous tutorials on these topics to learn more about them.
Our tutorial will be divided into three sections:
- Preparing the External Assets
- Creating the Graphical Interface in Flash
- Writing the Script
Preparing the External Assets
A Flash movie cannot communicate with a server or send an email message by itself. We need to use an external server-side script such as PHP or ASP to do this job. In this tutorial we will use a PHP script. Create a new folder for your project and inside this folder use any text tool a blank PHP file named mail.php.
This file will have the responsibility of receiving our message details from the SWF movie which we will create later and then send this data to the email address we specify. Simply paste the following PHP code to do this job.
$to = "yourAddress@domain.com";
$subject = ($_POST['senderName']);
$message = ($_POST['senderMsg']);
$message .= "\n\n---------------------------\n";
$message .= "E-mail Sent From: " . $_POST['senderName'] . " <" . $_POST['senderEmail'] . ">\n";
$headers = "From: " . $_POST['senderName'] . " <" . $_POST['senderEmail'] . ">\n";
if(@mail($to, $subject, $message, $headers))
Explaining the PHP code goes beyond the scope of this tutorial, however, the basic concept behind the code is that it assembles the information into a single message and then sends it to the email address you specify using the @mail function.
This should make our PHP script ready. Simply save it and launch Flash to start the next step.
Creating the Graphical Interface of the Form
Create a new FLA in Flash in AS3 format. Save it with the name ContactMovie in the same project folder as your PHP script.
We are now going to create the graphical elements we need for our contact form, namely:
- A text input field for the email address of the sender.
- A text input field for the name of the sender.
- A bigger text area field for the body of the message.
- A submit button.
We are going to use ready-made components for this job. Open up the Components panel by going through Window>Components and drag an instance of the TextInput component onto the stage. You will have to resize it a little bit to have it look like the image below:
Now that you have this field on the stage we will have to assign an instance name to it so that we can interact with it via ActionScript. While your field is still selected, access the Properties Inspector and set the Instance name of it to email_txt.
That should do it for the first field, repeat the same process to create another field for the name of the sender of the message, after creating the instance of the component, set the instance name of it to name_txt.
For the body of the message we need to insert an instance of the TextArea component on to the stage and set an instance name of it to message_txt.
Finally, the last component we need to add is the submit button, to do this simply drag a button component to the stage and set its instance name to submit_btn.
Our submit button is not fully ready yet as you might have noticed, we need to change the label so that the button actually says Submit. To do this access the Component Inspector by going through Window>Components Inspector and set the Label property of the button to Submit.
That should do it, you should have the following elements on your stage right now:
We clearly need more labels to explain what is what here, so use the Text Tool to write the right labels next to each of the fields. You should end up with something similar to the image below:
That should do it. All of our graphical elements are now done. We are going to script our contact form in the next section.
- Created the PHP file.
- Created the FLA file and placed all the visual elements on the stage.
In the next section of this tutorial we will write the ActionScript required to gather the data from our form and send it to our PHP script.
- End of Tutorial