AS2: ContactForm
View Example
View Documentation
Download Class & Example Files
Creates a communication gateway between a contact form and a PHP script that checks for validity and sends the email. The e-mail checking is rudimentary at best, but it gets the job done. If someone will go through the trouble to try and break this e-mail checker, then by all means spam away (at least that's the way I see it).
Actionscript:
-
import mx.events.EventDispatcher;
-
-
/**
-
* Creates a communication gateway between a contact form and a PHP script that checks for validity and sends the email. The
-
* e-mail checking is rudimentary at best, but it gets the job done. If someone will go through the trouble to try and break this
-
* e-mail checker, then by all means spam away (at least that's the way I see it).
-
*
-
* <p>The fields array should contain an object for each field. The object will have a "field" string value, which is the
-
* instance name (minus the "_txt" suffix) of the text field to check, an "error" string value which is the error text
-
* that is displayed when the error events are dispatched, and a "required" boolean that checks if the field is a
-
* required field.</p>
-
*
-
* <p>As of right now, the fields that are passed into the fields array should be written as the instance name of the field minus the _txt extension.
-
* For example, if the instance name is name_txt, you pass "name" as the field string value in the fields array.</p>
-
*
-
* <p>The class dispatches events depending on what happens. The events are as follows:</p>
-
*
-
* <ul>
-
* <li>onEmailError: Dispatched if the e-mail check returns an incorrect e-mail address</li>
-
* <li>onInvalidError: Dispatched when a required field fails the check (displays error from fields array object)</li>
-
* <li>onMessageSent: Dispatched when the message has been successfully sent</li>
-
* <li>onServerError: Dispatched if the server is unable to deliver the message</li>
-
* </ul>
-
*
-
* @usage
-
* <code>
-
* <pre>
-
import com.reintroducing.forms.ContactForm;
-
var listener:Object = new Object();
-
var scriptURL:String = "scripts/contactSubmit.php";
-
var toEmail:String = "matt@reintroducing.com";
-
var subject:String = "Testing the ContactForm Class";
-
var fields:Array = new Array( {field: "name", error: "Please enter your name.", required: true},
-
{field: "email", error: "Please enter a valid e-mail address.", required: true},
-
{field: "phone", error: "", required: false},
-
{field: "comments", error: "Please enter your comments.", required: true});
-
var cf:ContactForm = new ContactForm(fields, scriptURL, toEmail, subject);
-
cf.addEventListener("onEmailError", listener);
-
cf.addEventListener("onInvalidError", listener);
-
cf.addEventListener("onMessageSent", listener);
-
cf.addEventListener("onServerError", listener);
-
listener.onEmailError = function($eo:Object):Void
-
{
-
_root.error_txt.text = $eo.error;
-
};
-
listener.onInvalidError = function($eo:Object):Void
-
{
-
_root.error_txt.text = $eo.error;
-
};
-
listener.onMessageSent = function($eo:Object):Void
-
{
-
_root.error_txt.text = "Your message has been sent. Thanks!";
-
};
-
listener.onServerError = function($eo:Object):Void
-
{
-
_root.error_txt.text = "Server failed. Please try again later.";
-
};
-
submit_btn.onRelease = function():Void
-
{
-
_root.cf.sendMessage(_root);
-
};
-
* </pre>
-
* </code>
-
*
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.1
-
*/
-
-
class com.reintroducing.forms.ContactForm
-
{
-
/*
-
* ====================================================================================================
-
* PRIVATE VARIABLES
-
* ====================================================================================================
-
*/
-
-
// constants
-
private var _CLASS_NAME:String = "ContactForm";
-
-
private var _fields:Array;
-
private var _numFields:Number;
-
private var _scriptURL:String;
-
private var _email:String;
-
private var _subject:String;
-
-
/*
-
* ====================================================================================================
-
* PUBLIC VARIABLES
-
* ====================================================================================================
-
*/
-
-
public var msg:LoadVars;
-
public var msgSent:LoadVars;
-
-
// EventDispatcher mixins
-
public var addEventListener:Function;
-
public var removeEventListener:Function;
-
public var dispatchEvent:Function;
-
-
/*
-
* ====================================================================================================
-
* CONSTRUCTOR
-
* ====================================================================================================
-
*/
-
-
/**
-
* Creates a new instance of the ContactForm class. The events "onEmailError" and "onInvalidError" will be dispatched if
-
* there is an error with the e-mail field or any other required field. The errors that will be shown are defined in the
-
* fields array.
-
*
-
* @usage <pre><code>var cf:ContactForm = new ContactForm($fields, $scriptURL, $email, $subject);</code></pre>
-
*
-
* @param $fields An array that tells the form what fields to check, what errors to display, and if the field is required or not
-
* @param $scriptURL A string that represents the path on the server to the PHP script
-
* @param $email A string representing an e-mail address that the script will send the information to
-
* @param $subject A string representing the subject of the e-mail being sent
-
*/
-
-
public function ContactForm($fields:Array, $scriptURL:String, $email:String, $subject:String)
-
{
-
EventDispatcher.initialize(this);
-
-
_fields = $fields;
-
_numFields = _fields.length;
-
_scriptURL = $scriptURL;
-
_email = $email;
-
_subject = $subject;
-
}
-
-
/*
-
* ====================================================================================================
-
* PRIVATE METHODS
-
* ====================================================================================================
-
*/
-
-
private function validateEmail($str:String):Boolean
-
{
-
var after_at = $str.split("@");
-
var after_periods = after_at[1].split(".");
-
-
if (after_at.length != 2) return false;
-
if (after_periods.length <2 || after_periods.length> 3) return false;
-
-
return true;
-
}
-
-
// check the fields in the form and validate them
-
private function checkForm($r:MovieClip):Boolean
-
{
-
var missing:Boolean;
-
-
for (var i:Number = 0; i <_numFields; i++)
-
{
-
if (_fields[i].field == "email" && _fields[i].required)
-
{
-
// check the email field for validation
-
if (!validateEmail($r[_fields[i].field + "_txt"].text))
-
{
-
missing = true;
-
dispatchEvent({type: "onEmailError", target: this, field: _fields[i].field, error: _fields[i].error});
-
}
-
}
-
-
// check all other fields to see if they have something in them
-
if ($r[_fields[i].field + "_txt"].text == "" && _fields[i].field != "email" && _fields[i].required)
-
{
-
missing = true;
-
dispatchEvent({type: "onInvalidError", target: this, field: _fields[i].field, error: _fields[i].error});
-
}
-
}
-
-
// if missing is true, return false; otherwise return true
-
return missing ? false : true;
-
}
-
-
/*
-
* ====================================================================================================
-
* PUBLIC METHODS
-
* ====================================================================================================
-
*/
-
-
/**
-
* Checks the form and dispatches the appropriate event according to what is happening.
-
*
-
* @usage <pre><code>cf.sendMessage(_root);</code></pre>
-
*
-
* @param $root The timeline where the fields/clips for the form are located on
-
* @return Nothing
-
*/
-
-
public function sendMessage($root:MovieClip):Void
-
{
-
var tRef:Object = this;
-
var formValid:Boolean = checkForm($root);
-
-
msg = new LoadVars();
-
msgSent = new LoadVars();
-
-
msgSent.onLoad = function():Void
-
{
-
if (this.sent == "OK")
-
{
-
tRef.dispatchEvent({type: "onMessageSent", target: this});
-
}
-
else
-
{
-
tRef.dispatchEvent({type: "onServerError", target: this});
-
}
-
};
-
-
if (formValid)
-
{
-
// pass the email and subject that you are sending
-
msg.to = _email;
-
msg.subject = _subject;
-
-
// pass the appropriate values to the php form for submission
-
for (var i:Number = 0; i <_numFields; i++)
-
{
-
msg[_fields[i].field] = $root[_fields[i].field + "_txt"].text;
-
}
-
-
msg.sendAndLoad(_scriptURL + "?ck=" + new Date().getTime(), msgSent);
-
}
-
}
-
-
/*
-
* ====================================================================================================
-
* EVENT HANDLERS
-
* ====================================================================================================
-
*/
-
-
-
-
/*
-
* ====================================================================================================
-
* GETTERS & SETTERS
-
* ====================================================================================================
-
*/
-
-
-
-
/*
-
* ====================================================================================================
-
* HELPERS
-
* ====================================================================================================
-
*/
-
-
public function toString():String
-
{
-
return "[" + _CLASS_NAME + "]";
-
}
-
-
/*
-
* ====================================================================================================
-
* END CLASS
-
* ====================================================================================================
-
*/
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.













[...] is a direct port of my AS2 ContactForm with updates to work with AS3. The files come with a ContactFormEvent class which allows me to [...]