1 Attachment(s)
Creating an Email Form with Actionscript 3.0 and PHP
<p>Who uses Flash to create Web sites knows that the contact form (form of sending email) is one of the most used up. <br>
I received many e-mails by users who demanded a tutorial explaining how to build one. <br>
What I will explain then describes step by step how to create a basic form of sending email using Actionscript 3.0 and PHP.</p>
<h2>Step 1 - Create FLA and Document Class </h2>
<p>Create a FLA file and seve it as name "main.fla." <br>
Create the Document Class, an AS file named "Main.as", implemented in this way</p>
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");
}
}
}
</p>
<p>The code you see is the basic statement of a Document Class we have seen widely in other articles:</p>
<p>Document Class</p>
<p>Best way to instance the Document Class</p>
<p>Call from the timeline to the Document Class and vice versa</p>
<p>How to call another class from the Document Class</p>
<h2>Step 2 - Create graphics </h2>
<p>The first thing to do is create the graphics and objects that we need for our email-form. <br>
In this case, use 3 simple input text fields (name, email, message) and a sending button. <br>
Will be to customize each of you as fit. </p>
<p>In a level of my FLA I create 3 text fields of type input: <br>
- name_txt where user will insert his name <br>
- email_txt: user will insert his e-mail <br>
- message_txt: user will insert the message </p>
<p>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. <br>
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. <br>
How to do it, read the following tutorial: </p>
<p>- Http://blog.flepstudio.org/tutorials/incorporare-fonts-nell-swf-con-flash-cs3/</p>
<p>Now I create a sending button with instance name "send_btn". <br>
I select the three text fields and transform them into a MovieClip. <br>
I call the MovieClip mc_form and assign it the istance name of "form_mc".</p>
<p>I get the following results: </p>
http://blog.flepstudio.org/wp-conten...8/10/pic_1.jpg
<h2>Step 3 - Creating the Actionscript logics</h2>
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");
}
}
}
<p><strong>Let's navigate through the code:</strong></p>
<p>Add the listener to the button that when clicked calls a function that controls if user has filled each field:<br>
<em>form_mc.send_btn.addEventListener(MouseEvent.M OUSE_DOWN,checkFields);</em></p>
<p>I check if the fields have ben filled and I call a function that will send the values to PHP:<br>
<em>if(form_mc.name_txt.text!=""&&am p;form_mc.email_txt.text!=""&&fo rm_mc.message_txt.text!="")<br>
sendMessage();</em></p>
<p>Send the variables to PHP:</p>
<p>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.<br>
Important: These 3 variables have the same names of variables that I will set in PHP which it expectes via POST. <br>
For each of the three properties assign the text of the corresponding text field<br>
<em>var variables:URLVariables=new URLVariables();<br>
variables.name=form_mc.name_txt.text;<br>
variables.email=form_mc.email_txt.text;<br>
variables.message=form_mc.message_txt.text;</em></p>
<p>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.<br>
<em>var request:URLRequest=new URLRequest();<br>
request.url='sendMail.php';<br>
request.method=URLRequestMethod.POST;<br>
request.data=variables;</em></p>
<p>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".<br>
Add a listener that will call a function when the variables has been sent and PHP echo to us.<br>
<em>var loader:URLLoader=new URLLoader();<br>
loader.dataFormat=URLLoaderDataFormat.VARIABLES;<b r>
loader.addEventListener(Event.COMPLETE,messageSent );<br>
try <br>
{<br>
loader.load(request);<br>
} <br>
catch (error:Error) <br>
{<br>
trace('Unable to load requested document.');<br>
}</em></p>
<p>Obtaining PHP answer: <br>
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.<br>
Also read the following post: http://blog.flepstudio.org/tutorials/string-passed-to-urlvariablesdecode-must-be-a-url-encoded-query-string-containing-namevalue-pair/<br>
<em>var loader:URLLoader=URLLoader(evt.target);<br>
var vars:URLVariables=new URLVariables(loader.data);<br>
if(vars.answer=="ok")<br>
trace("The message has been sent");<br>
else<br>
trace("Something wrong");</em></p>
<p>Last this is the PHP:</p>
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:
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()
Re: Creating an Email Form with Actionscript 3.0 and PHP
hi,i did the steps but still geting the error.
i aint now much of the php but whats that "String must be blah blah"?
i dont know italian too so i coudnt understand your article abot error 2101!
please help me figure this one out ,man!!
Re: Creating an Email Form with Actionscript 3.0 and PHP
Hi there,
How does the external actionscript file work with the form_mc inside of the FLA file? Do I have to call it out with Actionscripting outside of the movie clip?
Re: Creating an Email Form with Actionscript 3.0 and PHP
Hi,
you have to test them up by uploading to a server of yours or any portals available in order to see if it works. Remember to change the domain address to your email or your website's domain mail.
Re: Creating an Email Form with Actionscript 3.0 and PHP
Hey. great tutorial!
I'm following from inside another movieclip, and am getting "Error #1009: Cannot access a property or method of a null object reference." on behalf of pedidos_mc.form_mc.send_btn.addEventListener(Mouse Event.MOUSE_DOWN,checkFields);
Said movieclip is brought to scene in frame 30, so I guess I'd have to add some sort of code for the event to be added after the movieclip is loaded, right? how do I do that (AS n00b here. thanks for your patience)
Re: Creating an Email Form with Actionscript 3.0 and PHP
ive created a form in flash and done the php script and uploaded it all to my server but when i send on the form i only get the last $msg heading but no info of any sort, please help?
Re: Creating an Email Form with Actionscript 3.0 and PHP
i cant get this to work. I have tried everything and it just wont work. Is anyone offering help on this?
Re: Creating an Email Form with Actionscript 3.0 and PHP
[QUOTE=muasif80;16741]I have got this error Can you please help in this regard
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: sendMail.php
at Main/sendMessage()
at Main/checkFields()
Re: Creating an Email Form with Actionscript 3.0 and PHP