Simple AS3/PHP Contact Form

The Nitty Gritty
Chris Bristol
I have been using Flash in my web development since version 4. As ActionScript became a more powerful language, I found myself fascinated by the interaction between it and PHP and MySQL. In recent years, I have worked at eliminating the use of the timeline altogether, building everything in pure AS3, and making my code reusable. The fantastic resources of the vast online Flash community has been a great source of help and inspiration. I hope to be able to give back a little of that. You can reach me via my website: Xty Digital Design
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 ContactForm.as 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:
[AS]
private var t7:TextField;
private var ip4:TextField;
[/AS]
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:
[AS]
t7 = new TextField();
t7.x = xPos;
t7.y = yPos;
t7.height = 20;
t7.width = 90;
t7.text = "Company Name:";
t7.setTextFormat(titleRFormat);
addChild(t7);
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);
addChild(ip4);
xPos -= 95;
yPos += 30;
[/AS]
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:
[AS]
if(ip4.text == ""){
statusTxt.text = "Please put in your Company name.";
statusTxt.setTextFormat(statusFormat);
ip4.backgroundColor = 0xFFFFCC;
break;
}
[/AS]
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]
variables.company = ip4.text;
[/AS]
Finally, to make sure we clean up everything, go to the clearForm function and add in additional fields to be cleared.
[AS]
private function clearForm( ):void {
ip1.text = "";
ip2.text = "";
ip3.text = "";
ip4.text = "";
ta.text = "";
charsIn.text = "";
buildChars();
}
[/AS]
The ContactForm.as 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.
[PHP]
<?php
$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 ContactForm.as
//$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.">");
?>
[/PHP]
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 ContactForm.as 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.
Spread The Word
Related Articles
Attachments
42 Responses to "Simple AS3/PHP Contact Form" 
|
said this on 05 Apr 2010 6:37:48 AM CDT
Great work! People have b
|
|
said this on 06 Apr 2010 3:15:56 PM CDT
I'm having a hard time us
|
|
said this on 06 Apr 2010 4:18:32 PM CDT
Hi Jessamyn
Create a new |
|
said this on 18 Apr 2010 1:34:39 PM CDT
A great tutorial, but str
|
|
said this on 18 Apr 2010 2:22:54 PM CDT
Hi Joe
Don't know what t |
|
said this on 04 May 2010 2:22:19 PM CDT
where do i download the f
|
|
said this on 04 May 2010 2:32:32 PM CDT
Hi starhawk85
Right at t |
|
said this on 04 May 2010 10:32:59 PM CDT
got it .. i wnet through
|
|
said this on 04 May 2010 10:56:24 PM CDT
Hi Starhawk85
I'm not su |
|
said this on 04 May 2010 11:30:39 PM CDT
i have a web page i am cr
|
|
said this on 05 May 2010 8:22:12 AM CDT
Hi Starhawk85
On the fra var cf:Con cf = new Conta cf.x = 20; cf.y addChild(cf); There is no If you have a |
|
said this on 06 May 2010 12:07:06 AM CDT
thats awsome! thank you t
|
|
said this on 06 May 2010 12:32:02 AM CDT
the only problem i have n
|
|
said this on 06 May 2010 10:11:51 AM CDT
How does the visitor move
remov That shoul |
|
said this on 06 May 2010 8:19:44 PM CDT
this is what i have for t
but where do i add i do have mult also i wou |
|
said this on 06 May 2010 9:26:22 PM CDT
Without knowing the struc
When you set up your con cf.name = "myForm"; if(getChildByName("myFor removeChild(cf); } So, if the contact fo |
|
said this on 06 May 2010 9:35:32 PM CDT
my as3 looks like this in
stop(); funct gotoAn } f go } function button3_click gotoAndStop ("page3") ; } function button4_c gotoAndStop ("page4 } function butto gotoAndStop ("p } function b gotoAndStop } functi gotoAnd } fu got } button1.addEventListene button2.add button3.addEventListener button4.addE button6.addEv b button8.addEve var cf:Co cf = new Cont cf.x = 320 cf.y = 210; addChild( |
|
said this on 06 May 2010 10:47:51 PM CDT
Change this:
var cf:Cont cf = new Contac cf.x = 320; cf.y = 210; addChild(cf To this: var cf:Co cf = new Cont cf.x = 320 cf.y = 210; cf.name = addChild(cf); Then, in each of your if(g removeChild(cf); } F function b if(getChildB removeC } gotoAndStop } This w |
|
said this on 07 May 2010 12:20:56 AM CDT
but yes i did all the cha
THANK YOU AGAIN C |
|
said this on 07 May 2010 12:26:22 AM CDT
never mind odd enough i r
|
|
said this on 07 May 2010 8:28:49 AM CDT
Hi Starhawk85
I'm glad y Th function button1_ if(getChildByName(" removeChild(cf } should have been function button1_cli if(getChildByName("myF removeChild(cf); } |
|
said this on 07 May 2010 12:50:20 PM CDT
no worries chris typos ha
this really is you have b i can |
|
said this on 07 May 2010 8:00:27 PM CDT
Hi Starhawk85
You are qu |
|
said this on 07 May 2010 10:26:16 PM CDT
thanks chris i have notic
|
|
said this on 24 Jun 2010 8:15:22 PM CDT
I was testing my form { h
All in all, a gr |
|
said this on 25 Jun 2010 10:36:48 AM CDT
Hello Philip
Nice implem The c if(ip2.text to include || ip2.text.l None of this |
|
said this on 27 Jun 2010 7:13:41 PM CDT
Thanks for the great tut,
|
|
said this on 28 Jun 2010 6:53:33 AM CDT
Hello penstar
AS3 classe Ho |
|
said this on 05 Jul 2010 9:37:51 PM CDT
Excell resources, keep it
|
|
said this on 02 Aug 2010 4:32:13 PM CDT
Thank you, this is fantas
W |
|
said this on 19 Oct 2010 1:29:33 PM CDT
I am looking for a script
---- send_mail.php <?php $con $contact_email = $_PO $contact_su $contact_message = $_P if( $c { $sender = $contact_emai $receiver = "myemail $client $email_body = "Na $extra = "From: $ if( mail( $receiver, " { ech } el { echo "success=n } } ?> ------- ActionScript ------ contact_name.text = con s reset_button.addEve var timer: var var_load:URLL v URL_request function submi { contact_subj { message_status.text = } else if( { mess } else { var e + + "&me var U URL_vars.dataFormat URL_request.da var_loa } } fu { contact_name. contact_message.text } function validat { var p:RegExp = /(\ var r:Object = if( r == nu { return false } return true; } function receive_respon { var var if( email_status { messag timer = new Timer(500 timer.addEventListe timer.start(); } else { messag } } function on_timer(te:Tim { if( t { contact_name.text timer.removeEventListe } } I would Thank you in ad |
|
said this on 13 Dec 2010 9:22:00 PM CDT
wonderful !
you helped m thank you so much |
|
said this on 05 Jan 2011 9:02:01 PM CDT
Hi Chris,
great work and Just a I've used the The swf come in a I import flash.ui.Keyb import caurina.tra import cauri ColorSh import f i import flash.net.URLL // import for c import flash import flas import flash. import flash.uti import fl.control import fl.con ----- // var mailLoader:Loade ma mailPanel. ma mai m ma fun m } // mail is mail.addE fun { Tweener.addTwee Tweener.ad function ad { m } } // c function re { Twee Tweene function remo { } In th On the server I You can see a test http://www. click on ITA button As I s If you try http://www.aino you can load directl Any suggestion is Thank |
|
said this on 05 Jan 2011 9:59:50 PM CDT
Hi Antonio
The Conta The Try Chris |
|
said this on 06 Jan 2011 8:28:44 AM CDT
Hi Chris,
thanks for sug I'll stay tuned for a Thanks agai |
|
said this on 20 May 2011 12:55:36 PM CDT
This is not secure!!
You A |
|
said this on 30 Jun 2011 11:21:04 AM CDT
Thanks very much. It's an
|
|
said this on 28 Jan 2012 6:46:27 PM CDT
Hi Chris, please help as
|
|
said this on 07 Mar 2012 7:33:22 AM CDT
Hi to every single one, i
contains pricel |
|
said this on 31 Dec 2012 9:30:31 AM CDT
A work mate recommended m
|
|
said this on 30 Jan 2013 1:47:39 PM CDT
Thank you for another gre
writing? I hav |


Author/Admin)
