Open up the file and we'll run through it quickly before taking a closer look at it. What we are doing in a nutshell is 1) Collecting information, 2) Verifying that we have all the information required and, 3) Passing that information to a PHP script that will in turn email it to us.

We start by declaring the variables we will need in this script. Then, from the constructor, we grab the parameters we have passed to the script and assign their values to the variables we have declared. Next we set up the physical parts, such as the textfields and buttons required to operate the script. The last thing we do here is call the buildChars function, which sets up our random Captcha code.

In the butHandler function we have 2 buttons to look after, so we use a switch statement to tell us which button has been pressed. In the case of a "New Code" being required for the Captcha feature, we simply call the buildChars function again to create a new random set of letters and numbers. If the "Submit" button has been pushed, we start the process of verifying the information that has been submitted. First we make sure that the Captcha code submitted is the same as the code that was randomly generated. If it is not, then we send a message to our status textfield and highlight the input field where the Captcha code is to be typed. Then we "break" to stop the processing of the form until the problem has been fixed. Next, we look at all the other textfields that we have decided are required, and make sure that something has been entered in them, and, in the case of the email, we can verify that it at least contains an "@" symbol and a ".". This will not ensure that it is a real email address, but at least we know that it is a validly constructed email address. Once all the hoops have been jumped through successfully, we call the sendData function.

In the sendData function we start by creating a URLVariable and assigning the information collected in the form to variables that will be sent to the PHP script. One of those variables is a message: variables.sendresponse = "Your message is being sent. Hang on...". This is displayed in the status textfield just to let the submitter know that something is going on in the background. Now we make a new URLRequest and assign the URLVariables to it's data property. The URLRequest's method property needs to be set to: URLRequestMethod.POST, so that we can deal with it properly in the PHP script. Next, we create a new URLLoader. Be sure and assign it's dataFormat property to ".TEXT". We have added two eventListeners to the URLLoader - Event.COMPLETE, sendComplete and IOErrorEvent.IO_ERROR, sendIOError . This will let us monitor the success or failure of the PHP script and allow us to pass a message back to the contact form. If everything goes according to plan, the sendComplete function will fire and let the submitter know that everything has worked beautifully. If there is a problem somewhere along the way, the sendIOError function will fire and let the submitter know that their message was not sent successfully.

The contact form as it is now is very simple. For your project you may require other information to be collected and passed on. Let's take a look and see how to do that.

Adding Additional Fields

When you looked through the file, and you will have noticed some additional fields which have been commented out. The example given is for a Company Name. What you need to keep in mind is that when you add additional fields to the contact form, you will need to add those fields into the contactForm.php file as well. In the contactForm.php file provided, there is also an example of adding an additional field. The other thing to keep in mind when adding more fields is that the height of the movie will increase. You will need to adjust the contactForm.fla or make allowances in your flash movie for this extra space.

Starting at the top of the file we first need to declare any additional fields like so:

private var t7:TextField;
private var ip4:TextField;

This will give us an additional title field and its' accompanying input field. Next we add in those additional fields in the appropriate spot in the code:

t7 = new TextField();
t7.x = xPos;
t7.y = yPos;
t7.height = 20;
t7.width = 90;
t7.text = "Company Name:";

xPos += 95;

ip4 = new TextField();
ip4.x = xPos;
ip4.y = yPos;
ip4.height = 20;
ip4.width = 255;
ip4.border = true;
ip4.type = "input";
ip4.background = true;
ip4.addEventListener(Event.CHANGE, txtBoxHandler);

xPos -= 95;
yPos += 30;

You can just copy and paste an entire block, and, because we are using the xPos and yPos variables to set the x and y parameters, you just plug that block of code in the most appropriate spot. For the Company Name example this would probably be under the "Your Name" text block. When you copy and paste additional blocks like this all you need to do is change their names from, say t7 to t8 and ip4 to ip5, and also the title text to whatever information it is you are collecting. Everything else will stay exactly the same.

Once your additional information fields are in place, you go down to the butHandler function, and under the Submit case you add in the following - if you are making this a required field:

if(ip4.text == ""){
statusTxt.text = "Please put in your Company name.";
ip4.backgroundColor = 0xFFFFCC;

Once your error checking section is ready, head down to the sendData function and add in the extra variables to move the collected information to the PHP script.

[AS] = ip4.text;

Finally, to make sure we clean up everything, go to the clearForm function and add in additional fields to be cleared.
private function clearForm( ):void {
ip1.text = "";
ip2.text = "";
ip3.text = "";
ip4.text = "";
ta.text = "";
charsIn.text = "";

The class is now fully modified and is sending all the information you have collected to the contactForm.php file. Now you need to visit that file and add in the extra fields there as well. Below is the entire PHP file with the additional example fields commented out.

$name = $_POST['fullname'];
//$company is an example of adding an extra field in the flash script - you must remember to also make this change in the flash class
//$company = $_POST['company'];
$toSubject = $_POST['subject'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$comment = stripslashes($comment);
$toEmail = $_POST['toEmail'];
$toName = $_POST['toName'];
$message = "From: $name\n\nReturn E-mail: $email\n\nMessage: $comment";
//Below is $message with the extra field of $company added in
//$message = "From: $name\n\nCompany: $company\n\nReturn E-mail: $email\n\nMessage: $comment";
mail($toName." <".$toEmail.">",$toSubject, $message, "From: ".$name." <".$email.">");

The End? No, the Beginning

What you are supposed to do now is take this and use it to build the many different type of forms you will use. These core concepts of gathering, verifying and passing along information should enable you to do just about anything you, or your clients, want. You can add CheckBoxes, ComboBoxes, Radio Buttons and they all work on the same set of principles.

I haven't explained all the code in the class exhaustively, and this oversight on the part of tutorial writers
used to drive me crazy. They always left out the one thing I really wanted to know. But now that I've actually written a
tutorial, I can see where the thing could easily turn into a book. If there is something here you would like a more detailed
explanation of, don't hesitate to contact me.