Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Creating an Email Form with Actionscript 3.0 and PHP

This is a discussion on Creating an Email Form with Actionscript 3.0 and PHP within the Tutorials forums, part of the Flash English category; Who uses Flash to create Web sites knows that the contact form (form of sending email) is one of the ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  9 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 28-10-08, 05:31
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Creating an Email Form with Actionscript 3.0 and PHP

Who uses Flash to create Web sites knows that the contact form (form of sending email) is one of the most used up.

I received many e-mails by users who demanded a tutorial explaining how to build one.

What I will explain then describes step by step how to create a basic form of sending email using Actionscript 3.0 and PHP.


Step 1 - Create FLA and Document Class


Create a FLA file and seve it as name "main.fla."

Create the Document Class, an AS file named "Main.as", implemented in this way


Code:
package
{
	import flash.display.*;
	import flash.events.*;
	
	public class Main extends MovieClip
	{
		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			trace("My Document Class has been created correctly");
		}
	}
}


The code you see is the basic statement of a Document Class we have seen widely in other articles:


Document Class


Best way to instance the Document Class


Call from the timeline to the Document Class and vice versa


How to call another class from the Document Class


Step 2 - Create graphics


The first thing to do is create the graphics and objects that we need for our email-form.

In this case, use 3 simple input text fields (name, email, message) and a sending button.

Will be to customize each of you as fit.


In a level of my FLA I create 3 text fields of type input:

- name_txt where user will insert his name

- email_txt: user will insert his e-mail

- message_txt: user will insert the message


For each of these 3 fields as you see I have assigned an instance name (name_txt, email_txt, message_txt) so I can refer to them by the Document Class.

Also I need the characters and the font we use are visible by all, make sure to include the right set of characters for each text field.

How to do it, read the following tutorial:


- Http://blog.flepstudio.org/tutorials/incorporare-fonts-nell-swf-con-flash-cs3/


Now I create a sending button with instance name "send_btn".

I select the three text fields and transform them into a MovieClip.

I call the MovieClip mc_form and assign it the istance name of "form_mc".


I get the following results:



Step 3 - Creating the Actionscript logics


Code:
package
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.net.*;
	
	public class Main extends MovieClip
	{
		public function Main()
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,init);
			
			trace("My Document Class has been created correctly");
			
			form_mc.send_btn.addEventListener(MouseEvent.MOUSE_DOWN,checkFields);
		}
		
		private function checkFields(evt:MouseEvent):void
		{
			if(form_mc.name_txt.text!=""&&form_mc.email_txt.text!=""&&form_mc.message_txt.text!="")
				sendMessage();
		}
		
		private function sendMessage():void
		{
			var variables:URLVariables=new URLVariables();
			variables.name=form_mc.name_txt.text;
			variables.email=form_mc.email_txt.text;
			variables.message=form_mc.message_txt.text;
			
			var request:URLRequest=new URLRequest();
			request.url='sendMail.php';
			request.method=URLRequestMethod.POST;
			request.data=variables;
			
			var loader:URLLoader=new URLLoader();
			loader.dataFormat=URLLoaderDataFormat.VARIABLES;
			loader.addEventListener(Event.COMPLETE,messageSent);
			try 
			{
				loader.load(request);
			} 
			catch (error:Error) 
			{
				trace('Unable to load requested document.');
			}
		}
		
		private function messageSent(evt:Event):void
		{
			var loader:URLLoader=URLLoader(evt.target);
			var vars:URLVariables=new URLVariables(loader.data);
			if(vars.answer=="ok")
				trace("The message has been sent");
			else
				trace("Something wrong");
		}
	}
}

Let's navigate through the code:


Add the listener to the button that when clicked calls a function that controls if user has filled each field:

form_mc.send_btn.addEventListener(MouseEvent.MOUSE _DOWN,checkFields);


I check if the fields have ben filled and I call a function that will send the values to PHP:

if(form_mc.name_txt.text!=""&&form_mc.email_txt.te xt!=""&&form_mc.message_txt.text!="")

sendMessage();


Send the variables to PHP:


The first block of code of sendMessage function I use the URLVariables class.
I assign three properties at URLVariables istance and I call them name, email and message.

Important: These 3 variables have the same names of variables that I will set in PHP which it expectes via POST.

For each of the three properties assign the text of the corresponding text field

var variables:URLVariables=new URLVariables();

variables.name=form_mc.name_txt.text;

variables.email=form_mc.email_txt.text;

variables.message=form_mc.message_txt.text;


In the second block I use URLRequest class that makes a request of Flash to call a url ( the PHP file ). Set to POST the "how to send" variables mode method.

var request:URLRequest=new URLRequest();

request.url='sendMail.php';

request.method=URLRequestMethod.POST;

request.data=variables;


In the third block I use URLLoader class that will call and send to PHP the values of the URLVariables we have previously assigned. Assign to dataFormat property of Loader the ownership of the string URLLoaderDataFormat.VARIABLES loader, which is a string "variables".

Add a listener that will call a function when the variables has been sent and PHP echo to us.

var loader:URLLoader=new URLLoader();

loader.dataFormat=URLLoaderDataFormat.VARIABLES;

loader.addEventListener(Event.COMPLETE,messageSent );

try

{

loader.load(request);

}

catch (error:Error)

{

trace('Unable to load requested document.');

}


Obtaining PHP answer:

In messageSent function recovery the response of the PHP by using same loader that I used to send and creating an instance of URLVariables that will contain a variable named answer with a value assigned by PHP. If the value is "ok" then execute the necessary logic, otherwise trace an error.

Also read the following post: http://blog.flepstudio.org/tutorials/string-passed-to-urlvariablesdecode-must-be-a-url-encoded-query-string-containing-namevalue-pair/

var loader:URLLoader=URLLoader(evt.target);

var vars:URLVariables=new URLVariables(loader.data);

if(vars.answer=="ok")

trace("The message has been sent");

else

trace("Something wrong");


Last this is the PHP:


PHP Code:
<?php
$to 
"address@mydomain.com";
$subject = ($_POST['name']);
$message = ($_POST['message']);
$message .= "\n\n---------------------------\n";
$message .= "E-mail inviata da: " $_POST['name'] . " <" $_POST['email']  . ">\n";
$headers "From: " $_POST['name'] . " <" $_POST['email'] . ">\n";
if(@
mail($to$subject$message$headers))
{
    echo 
"answer=ok";

else 
{
    echo 
"answer=error";
}
?>
Source files:
Attached Files
File Type: zip EmailForm.zip (563.7 KB, 197 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !

Last edited by Flep; 28-10-08 at 05:36..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 01-11-08, 15:59
Junior Member
 
Join Date: Nov 2008
Posts: 1
Rep Power: 0
muasif80 is on a distinguished road
Re: Creating an Email Form with Actionscript 3.0 and PHP

I have got this error Can you please help in this regard

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at flash.net::URLLoader/onComplete()
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Tags
creating, email form, flash and php

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Email Form PRO Flep Utilità di FlepStudio 54 18-12-08 18:32
Email form Flep FlepStudio utilities 27 16-12-08 02:35
Email Form PRO Flep FlepStudio utilities 36 25-11-08 14:36
Creare una form di invio email con Actionscript 3.0 e PHP Flep Articoli e tutorials 0 28-10-08 05:26
mod email form teonji AIUTO utilità free 1 25-07-08 13:02


All times are GMT. The time now is 15:40.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap