ActionScript
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.
That's all you need.
The Document Class ContactForm.as contains the following code:
package
{
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.";
return;
}
if (email == '') {
status_txt.text = "You need to fill in your E-mail.";
return;
}
// you should also validate the email address
if (comment == '') {
status_txt.text = "Please, don't forget your Comment!";
return;
}
// 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
loader.load(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.
Note:
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. |
Download (37 kb) |
» Forums |
More help? Search our boards for quick answers! |