First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Nuno Mira | Website: http://www.nunomira.com/


In Flash you build a simple layout with three input text fields, a button and a dynamic text field to provide info about what's going on.

flash form layout

That's all you need.

The Document Class ContactForm.as contains the following code:

	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.TextField;
	public class ContactForm extends Sprite
		public var email_txt:TextField;
		public var name_txt:TextField;
		public var comments_txt:TextField;
		public var status_txt:TextField;
		public var submit_btn:SimpleButton;
		private var loader:URLLoader;
		public function ContactForm()
			status_txt.text = 'All fields required';
			submit_btn.addEventListener(MouseEvent.CLICK, submit);
		 * when the submit button is clicked preform some validation
		 * @param	e
		private function submit(e:MouseEvent):void 
			// grab the content of the text fields
			var email:String = email_txt.text;
			var name:String = name_txt.text;
			var comment:String = comments_txt.text;
			// are all the fields filled?	
			if (name == '') {
				status_txt.text = "You need to fill in your Name.";
			if (email == '') {
				status_txt.text = "You need to fill in your E-mail.";
			// you should also validate the email address
			if (comment == '') {
				status_txt.text = "Please, don't forget your Comment!";
			// yes, all fields filled
			sendEmail(name, email, comment);
			// sending data...
			status_txt.text = "Processing mail form...";
			// prevent submitting again by disabling the button
			submit_btn.mouseEnabled = false;
		 * send the variables to the php script
		 * @param	name
		 * @param	email
		 * @param	comment
		private function sendEmail(name, email, comment)
			// create the request
			var request:URLRequest = new URLRequest();
			request.method = URLRequestMethod.POST;
			request.url = "script.php";
			// add the variables to the request
			var variables:URLVariables = new URLVariables();
			variables.name = name;
			variables.email = email;
			variables.comment = comment;
			request.data = variables;

			// create the loader
			loader = new URLLoader();
			// we'll be loading text
			loader.dataFormat = URLLoaderDataFormat.TEXT;

			loader.addEventListener(Event.COMPLETE, loaded);
			loader.addEventListener(IOErrorEvent.IO_ERROR, error);
			// send the request
		 * when the request is completed
		 * @param	e
		private function loaded(e:Event):void 
			// show the message
			status_txt.text = loader.data;
			submit_btn.mouseEnabled = true;
		 * if there's an error
		 * @param	e
		private function error(e:IOErrorEvent):void 
			status_txt.text = "Error! Try again please...";
			submit_btn.mouseEnabled = true;

Couldn't be simpler:
When the submit button is clicked, you get the contents of the text fields and make sure they aren't empty. The data is sent to the PHP Script using the URLLoader Class via POST (which is what the PHP script is expecting).
As the PHP script is outputting text only (not variable value/name pairs) the URLoaderDataFormat is set to TEXT.
When the request is completed the response is displayed in the status text field.


In order to play safe a relative URL is used. If you want to test directly from the Flash IDE, you can use an absolute one such as:

request.url = "http://localhost/flashkit/form/script.php";

When you go online, if you do need an absolute URL don't forget to add the Cross Domain Policy file to the server where the PHP script is.

» Level Intermediate

Added: 2011-03-18
Rating: 1 Votes: 1
» Author
Nuno Mira has been a Flash Developer for 9 years. He loves teaching, and learning. When he isn't coding he may be surfing or snowboarding.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.