PDA

View Full Version : Basic AS3 Contact Form



daidai
July 3rd, 2008, 07:10 PM
I stripped the one from here http://www.flepstudio.org/forum/flepstudio-utilities/2265-email-form-pictures-flash-cs3-php.html, which was a bit heavy for my needs...

package {

import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.AntiAliasType;
import flash.text.TextFieldAutoSize;
import flash.text.TextFieldType;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.net.URLVariables;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequestMethod;
import flash.events.IEventDispatcher;
import flash.utils.Timer;

public class ContactForm extends Sprite {

private var txtName:TextField = new TextField ();
private var txtMessage:TextField = new TextField ();
private var txtEmail:TextField = new TextField ();
private var txtSend:TextField = new TextField ();
private var inputName:TextField = new TextField ();
private var inputMessage:TextField = new TextField ();
private var inputEmail:TextField = new TextField ();
private var txtInfo:TextField = new TextField ();
private var format:TextFormat = new TextFormat ();
private var iformat:TextFormat = new TextFormat ();
private var sformat:TextFormat = new TextFormat ();
private var submitBtn:Sprite = new Sprite();
private var sendMailURL:URLRequest=new URLRequest("sendMail.php");
private var _name:String="";
private var _message:String="";
private var _email:String="";
private var timer:Timer;

public var uploadURL:URLRequest;
public const absoluteURL:String="";

public function ContactForm ()
{
addEventListener(Event.ADDED_TO_STAGE,init);
}

private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE,init);

format.size = 12;
format.font = 'Arial';
format.color = 0x333333;
format.align = 'left';

iformat.size = 12;
iformat.font = 'Arial';
iformat.color = 0xFF0000;
iformat.align = 'left';

sformat.size = 12;
sformat.font = 'Arial';
sformat.color = 0xFFFFFF;
sformat.align = 'left';

createTexts();
}

private function createTexts ():void
{
txtInfo.defaultTextFormat = iformat;
txtInfo.antiAliasType = AntiAliasType.ADVANCED;
txtInfo.autoSize = TextFieldAutoSize.LEFT;
txtInfo.wordWrap = true;
txtInfo.multiline = true;
txtInfo.selectable = false;
txtInfo.text = "";
txtInfo.x = 10;
txtInfo.y = 10;
txtInfo.width = 400;
addChild(txtInfo);

txtName.defaultTextFormat = format;
txtName.antiAliasType = AntiAliasType.ADVANCED;
txtName.autoSize = TextFieldAutoSize.LEFT;
txtName.wordWrap = true;
txtName.multiline = true;
txtName.selectable = false;
txtName.text = "NAME";
txtName.x = 10;
txtName.y = txtInfo.y + txtInfo.height + 30;
addChild(txtName);

inputName.defaultTextFormat = format;
inputName.text ="";
inputName.width =200;
inputName.height = 20;
inputName.x = txtName.x + txtName.width + 10;
inputName.y = txtName.y;
inputName.border = true;
inputName.type = TextFieldType.INPUT;
addChild(inputName);

txtEmail.defaultTextFormat = format;
txtEmail.antiAliasType = AntiAliasType.ADVANCED;
txtEmail.autoSize = TextFieldAutoSize.LEFT;
txtEmail.wordWrap = true;
txtEmail.multiline = true;
txtEmail.selectable = false;
txtEmail.text = "EMAIL";
txtEmail.x = 10;
txtEmail.y = txtName.y + txtName.height + 10;
addChild(txtEmail);

inputEmail.defaultTextFormat = format;
inputEmail.text ="";
inputEmail.width =200;
inputEmail.height = 20;
inputEmail.x = txtEmail.x + txtEmail.width + 10;
inputEmail.y = txtEmail.y;
inputEmail.border = true;
inputEmail.type = TextFieldType.INPUT;
addChild(inputEmail);

txtMessage.defaultTextFormat = format;
txtMessage.antiAliasType = AntiAliasType.ADVANCED;
txtMessage.autoSize = TextFieldAutoSize.LEFT;
txtMessage.wordWrap = true;
txtMessage.multiline = true;
txtMessage.selectable = false;
txtMessage.text = "MESSAGE";
txtMessage.x = 10;
txtMessage.y = txtEmail.y + txtEmail.height + 20;
addChild(txtMessage);

inputMessage.defaultTextFormat = format;
inputMessage.text ="";
inputMessage.width =200;
inputMessage.height = 150;
inputMessage.x = txtMessage.x + txtMessage.width + 10;
inputMessage.y = txtMessage.y;
inputMessage.border = true;
inputMessage.type = TextFieldType.INPUT;
inputMessage.multiline = true;
inputMessage.wordWrap = true
addChild(inputMessage);

submitBtn.graphics.beginFill (0x333333);
submitBtn.graphics.drawRect (txtMessage.x + txtMessage.width + 10, inputMessage.y + inputMessage.height + 20,100, 25);
submitBtn.graphics.endFill ();
submitBtn.buttonMode = true;
submitBtn.useHandCursor = true;
submitBtn.mouseChildren = false;
submitBtn.addEventListener(MouseEvent.MOUSE_DOWN, buttonHandler);
addChild(submitBtn);

txtSend.defaultTextFormat = sformat;
txtSend.antiAliasType = AntiAliasType.ADVANCED;
txtSend.autoSize = TextFieldAutoSize.LEFT;
txtSend.wordWrap = true;
txtSend.multiline = true;
txtSend.selectable = false;
txtSend.text = "SEND";
txtSend.x = txtMessage.x + txtMessage.width + 40;
txtSend.y = inputMessage.y + inputMessage.height + 23;
submitBtn.addChild(txtSend);
}

private function buttonHandler(event:MouseEvent):void
{
startSubmitSequence();
}

private function startSubmitSequence():void
{
// assign inputted text
_name = inputName.text;
_message = inputMessage.text
_email = inputEmail.text

if(checkRequiredFields()) // Check if all the required fields have text
if (isValidEmail(_email)) // Check if the Email entered is a valid one
sendMessage();
else
txtInfo.text = "NOT A VALID EMAIL ADDRESS";
else
txtInfo.text = "ALL FIELDS ARE REQUIRED";
}

private function isValidEmail(email:String):Boolean
{
var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return emailExpression.test(email);
}

private function checkRequiredFields():Boolean
{
if(_name!="" && _message!="" && _email!="")
return true;
else
return false;
}

private function sendMessage():void
{
var variables:URLVariables=new URLVariables();
variables.abs_url=absoluteURL;
variables.name = _name;
variables.message = _message;
variables.email = _email;

sendMailURL.method=URLRequestMethod.POST;
sendMailURL.data=variables;
var loader:URLLoader=new URLLoader();
loader.dataFormat=URLLoaderDataFormat.VARIABLES;
addListeners(loader);
try
{
txtInfo.text = "SENDING..."
loader.load(sendMailURL);
}
catch (error:Error)
{
txtInfo.text ="AN ERROR HAS OCCURRED PLEASE TRY AGAIN";
}
}
private function addListeners(d:IEventDispatcher):void
{
d.addEventListener(Event.COMPLETE,messageSent);
}

private function messageSent(event:Event):void
{
txtInfo.text = "THANKS, YOUR MESSAGE HAS BEEN SENT";

var loader:URLLoader=URLLoader(event.target);
var vars:URLVariables=new URLVariables(loader.data);
if(vars.answer=="ok")
{
timer=new Timer(4000,1);
timer.addEventListener(TimerEvent.TIMER,resetAll);
timer.start();
}
else
{
txtInfo.text = "AN ERROR HAS OCCURRED PLEASE TRY AGAIN";
}
}

private function resetAll(event:TimerEvent):void
{
timer.removeEventListener(TimerEvent.TIMER,resetAl l);
txtInfo.text = "";
inputName.text ="";
inputEmail.text ="";
inputMessage.text ="";
}
}
}

and the sendMail.php file, in this example saved in the same folder as the flash file

<?php
$sendTo = "example@email.com";
$subject = "E-mail from my web site";

$headers = "From: " . $_POST["name"] ."<" . $_POST["email"] .">\r\n";

$message = "";
$message .= "Name: ";
$message .= "\n";
$message .= $_POST["name"];
$message .= "\n\n";
$message .= "From: ";
$message .= "\n";
$message .= $_POST["email"];
$message .= "\n\n";
$message .= "\n\n";
$message .= "Message: ";
$message .= "\n";
$message .= $_POST["message"];
$message .= "\n\n";


if(@mail($sendTo, $subject, $message, $headers))
{
$answer='ok';
echo "answer=".$answer;
}
else
{
$answer='error';
echo "answer=".$answer;
}
?>

Example: contactform example (http://www.14lox.com/examples/contactform/contactform.html)

sekasi
July 3rd, 2008, 07:21 PM
I hate actionscript when you need to do text with it. Hate it with a passion.

Can't wait for FP10. Good job btw!

daidai
July 3rd, 2008, 07:27 PM
I hate actionscript when you need to do text with it. Hate it with a passion.

Can't wait for FP10. Good job btw!
sekasi, hows Flash player 10 going to help things?

sekasi
July 3rd, 2008, 07:55 PM
I meant CS4, sorry. It will re-vamp the way Flash works with fonts.

daidai
July 4th, 2008, 05:10 AM
I meant CS4, sorry. It will re-vamp the way Flash works with fonts.
yeh? could you point me in the direction of some docs about it, thanks.

sekasi
July 4th, 2008, 11:54 AM
No idea. I went to FiTC Chicago and heard it directly from Adobe during their CS4 preview presentation :)

prg9
July 4th, 2008, 12:23 PM
yeh? could you point me in the direction of some docs about it, thanks.
Here is a good write-up and video. However it might not be all inclusive at this point.

Flash Player 10 feature: New text engine
http://www.flashmagazine.com/news/detail/flash_player_10_feature_new_text_engine/

Video - Flash Player 10 Text Features
http://labs.adobe.com/technologies/flashplayer10/demos/videos/text.html

Templarian
July 5th, 2008, 12:51 AM
I hate actionscript when you need to do text with it. Hate it with a passion.

Can't wait for FP10. Good job btw!
It will still use the same AS I believe, they have just extended the amount of properties and things one can do with the textfield.

I don't see whats so bad with the textfield it's very easy to use. I myself wrote up a function to create the textfields to make it easier (basically sets all the default and obvious properties and places it at on the stage/somewhere defined in the parms). So really you can just make it easier for yourself.

saxx
July 17th, 2008, 03:25 AM
I know this post is old, but i F'ING love you! Thanks alot

Woah it doesn't need a pop3 server, cool

daidai
July 17th, 2008, 05:18 AM
wow, is two weeks old in AS3 terms? i must keep up...;)
glad it could help, saxx :proud:

daidai
September 20th, 2008, 02:09 PM
added email validation

bootiteq
September 21st, 2008, 05:19 AM
that is a sweet example.

I hope some day soon Adobe adds an editing interface for Sprites cause adding a library instance would save heaps of code in a lot of different cases without the need to create a component






______________

sekasi
September 22nd, 2008, 04:19 AM
I don't see whats so bad with the textfield it's very easy to use.

Easy to use doesn't make it good though. The way flash handles font is very 1922 and has massive amount of weaknesses. Especially because using library embedded dynamic textfields has major shortcomings when it comes to kearning and leading and library embedded fonts have major shortcomings as well since you can't specify what characters you wish to embed.

For us with full on asian libraries of the fonts this means a 60kb swf file that says "hello world" in some cases which just does not sit very well with me.

prg9
September 22nd, 2008, 10:50 AM
......and library embedded fonts have major shortcomings as well since you can't specify what characters you wish to embed. For us with full on asian libraries of the fonts this means a 60kb swf file that says "hello world" in some cases which just does not sit very well with me.

:h: @ sekasi

Straight from the Help Docs:


Rather than embedding entire character sets, you can create a custom character set that contains only the characters that you need. This way you can keep the size of your SWF file as small as possible, because you don't store any extra font information for the characters that you don't need.

Creating custom character sets -- Flash CS3
http://livedocs.adobe.com/flash/9.0/main/00000894.html

Creating custom character sets -- Version 8
http://livedocs.adobe.com/flash/8/main/00001431.html

:book:

You can create/modify/choose to embed only one letter if you want. How does that sit with you? Unless I misread your meaning ;-)

bootiteq
September 24th, 2008, 10:24 AM
I see your point but in a way it also highlights a weakness in flash fonts. Yes that is cool but to implement something like that across heaps of sites with a big flash team adds a hassle which shouldn't be there in the first place. Its way easy to add and embed fonts in Flex. Why not in flash? The program is centered around a User Interface isnt it?

I dont know how to go off topic sorry...

daidai
June 1st, 2009, 11:45 AM
I've updated this with clearer and commented code on my new blog http://blog.14lox.com/?p=9