Posted in Adobe, Flash
07/7 2006

AS2 :: Guestbook Class

Goal: To devise an easy way to add user interaction to a full-Flash site, in this case a guestbook.

class org.thesourcecode.utils.Guestbook
{
	private var _earl:String;
	private var _display;
	private var _getEntries:LoadVars;
	private var _sender:LoadVars;
	private var _receiver:LoadVars;
	private var _refresh:LoadVars;
	private var _mainTL:MovieClip;
	private var _validname:Boolean;
	private var _validemail:Boolean;
	private var _validcity:Boolean;
	private var _validmsg:Boolean;
	private var _array:Array;
	private var _uri:String;
 
	public function Guestbook( target:MovieClip )
	{
		_mainTL = target;
	}//end constructor
 
	//retreive the specified posts
	public function getPosts( url:String, target, min:Number, max:Number )
	{
		_earl = url;
		_display = target;
		_getEntries = new LoadVars();
		_getEntries.onLoad = function( success:Boolean )
		{
			if (success)
		{
				target.text = this.strflash;
			} 
		else 
		{
				target.text = this.strflash;
			}
		}
		_getEntries.load(_earl);
	}
 
	//get the user-entered info and insert into the database
	public function addPost( fields:Array, url:String,target )
	{
		_array = fields;
		_uri = url;
		_display = target;
		checkPost();
	}
 
	//basic validation stuff
	private function checkPost()
	{
		_validname = false;
		_validemail = false;
		_validcity = false;
		_validmsg = false;
		if (_mainTL.gname.text != "")
		{
			_validname = true;
		} 
		else 
		{
			_mainTL.msg.text = "Please complete all fields"
		}
		if (_mainTL.gmail.text == "") 
		{
			_mainTL.msg.text = "I need your email address";
		} 
		else if (_mainTL.gmail.text.indexOf("@") == -1 
		   || _mainTL.gmail.text.indexOf(".") == -1) 
		{
			_mainTL.msg.text = "Please provide a valid email";
		} 
		else
		 {
			_validemail = true;
		}
		if (_mainTL.gfrom.text != "")
		{
			_validcity = true;
		} 
		else 
		{
			_mainTL.msg.text = "Please complete all fields"
		}
		if (_mainTL.gmsg.text != "")
		{
			_validmsg = true;
		} 
		else 
		{
			_mainTL.msg.text = "Please complete all fields"
		}
		processCheck();
	}
 
	//2nd phase of validation (puts 2 steps between the FLA and class
	private function processCheck()
	{
		if (_validname && _validemail && _validcity && _validmsg)
		{
			_addPost( _array,_uri,_display );
		}
	}
 
	//now you can send the data
	private function _addPost( flds:Array,script:String,target )
	{
		_sender = new LoadVars();
		_receiver = new LoadVars();
 
		_receiver.onLoad = function()
		{
			_mainTL.msg.text = this.statusMsg;
		}/**/
 
		for (var i=0; i < flds .length; i++)
		{
			_sender[ flds[i] ] = _mainTL[ flds[i] ].text;
			trace( _sender[flds[i]] ); //use for debugging
			_mainTL[ flds[i] ].text = "";
		}
		_sender.sendAndLoad( script,_receiver,"POST" );
 
		getPosts( _earl,target );
	}
 
}//end class

Download the easy-install extension and sample files here
Documentation can be found here.

This Post tags: ,

 

USER COMMENTS

Track comments via RSS 2.0 feed. Feel free to post the comment, or trackback from your web site.

Currently there are no comments related to article "AS2 :: Guestbook Class".

Get Adobe Flash playerPlugin by wpburn.com wordpress themes