Simple flash AS3 contact form with field validation

This is an example of how AS3 aint all that bad.
I simply converted an already existing AS2 contact form to AS3.
Looking at the fla you will see that not much has changed.
The only major difference is how you setup sending your vars to the php script.
An example can be seen here.

I have also attached a working fla with php script.
Download AS contact form Version 0.3

Need a custom contact form built?
Feel free to email me

This entry was posted in Flash AS3, Flash CS3 components and tagged , , , , , , . Bookmark the permalink.

186 Responses to Simple flash AS3 contact form with field validation

  1. Steve says:

    Hi Paul,

    Thanks for the form. It works quality. I have hosting with 1 and 1 and got it to work perfect 1st time so I must thankyou.

    However, I am also using hosting with Freedom 2 surf. I have changed nothing and can’t get it to work!

    I have contacted them and they have a linux server and PHP is supported and the PHP file can be anywhere on my server.

    I have read some comments above about putting the full path to the PHP in my flash file and tried loads of different things but still no joy.

    Hope you have some advice.
    Cheers
    Steve

  2. Paul Ferrie says:

    Hi,

    Some server configurations require the email address used to send the email in the form is that of the same domain name the script is on.
    so for example you might have it set up like
    http://www.somedomain.com and in the script you have random@email.com. some servers don’t like this.
    If this is what your doing then try adding ini_set (“sendmail_from”,”your@email.com”);
    Obviously changing your@ for your own email.

    If this is not the case can you send me a direct link to the php script on the server. DO NOT paste the link into the post. Email it to me via my email form.

    Regards,
    Paul

  3. Steve says:

    Hi Paul,

    Thanks for your comments. I have just sent you a link to my php file on the server.

    I am new to PHP and not sure about the ini_set….

    Hope you can help.

    Thanks
    Steve

  4. Steve says:

    Hi Paul,

    Thanks for your email. please see this link

    http://www.freedom2surf.net/help/webhostingtechfaq.php

    Hope you cans shed some light!
    Steve

  5. Steve says:

    Hi Paul,

    Thanks for all your help yesterday. I am not getting loads of emials coming through but the flash file remains on the screen ‘sending mail…’

    It doesn’t get to the form submitted part!

    Hope you can help.
    Steve

  6. Paul Ferrie says:

    Change the path in the fla from http://www.yourdomain.com/contact.php to just contact.php

  7. Steve says:

    I Now get the error message but the mail still comes through!

  8. em says:

    I’ve been racking my brains out since yesterday and still no ideas are coming. people have been helping me and giving me bits of codes but still, nothing works…

    but this is PERFECT! finally got it to work . . .

    thanks a lot for sharing . . .

  9. Paul says:

    Mate – a big big thanks to you!! I’m new to AS3 and I needed to see some working code so I could debug a simple form I’m working on. (Well, it was supposed to be simple…) Cheers for the headsup – from a fellow Scot in Australia!

  10. Chong says:

    Hi I am getting the following error:

    I am using WAMP server and have put the swf file and contact.php in the same root www directory.

    Load failed: HTTP Status = 0
    Load failed: IO error: Error #2032: Stream Error. URL: localhost:80/contact.php

    Please help.
    Thank you!

  11. Paul Ferrie says:

    Sorry can’t comment on any files running on WAMP. All my files have been tested on both Linux and windows dedicated servers without any issue’s. That’s all I can say.

  12. Chong says:

    sorry i changed the following

    var req:URLRequest = new URLRequest(“http://127.0.0.1:80/contact.php”);

    but i’m getting ** Error sending mail **

    I did a trace(evt.target.data)
    and found the following
    %3Cbr%20%2F%3E%0A%3Cb%3ENotice%3C%2Fb%3E%3A%20%20Undefined%20variable%3A%20msgPost%20in%20%3Cb%3EC%3A%5Cwamp%5Cwww%5Ccontact%2Ephp%3C%2Fb%3E%20on%20line%20%3Cb%3E18%3C%2Fb%3E%3Cbr%20%2F%3E%0Aretval=1

    I am not getting any emails through.

    how should i change the contact.php?

    Thank you

  13. Paul Ferrie says:

    Yeah this is the readable error

    Notice: Undefined variable: msgPost in C:\wamp\www\contact.php on line 18

    So you have not passed msgPost in flash to the php script.

  14. Michal from Slovakia says:

    Hi Paul,
    thx for code but I’ve got a problem with ff. I tried it in IE and only one time I received the msg from the form. I’m using CS4. Any idea?

    Thank you

  15. Paul Ferrie says:

    Hi,
    Sorry without seeing what modifications you have made to the file or a link to somewhere i can test myself it is hard to say what your particular issue is.

  16. Paul Ferrie says:

    ok it might be easier if you just send me the fla and php script.

  17. Michal (SVK) says:

    I’ve solved the problem. Thx a lot for your interest.

  18. Paul Ferrie says:

    What was the problem with the contact form?

  19. Michal (SVK) says:

    I tried to implement form to the existing project. May be I did there some mystake. Actually I have a two projects.In the 1st project I just put a link via button to the 2nd project.

  20. Tristan says:

    I tried out the contact form, and it works great. But when the Email is received, the sender’s Email address is the same as my own. I’m confused. Shouldn’t it be the one entered into the input field?

  21. Paul Ferrie says:

    Change in the php script line 29
    $ok = mail($to, $ToSubject, $Message, $headers . “From:$senderName < ".$to.">“);
    To
    $ok = mail($to, $ToSubject, $Message, $headers . “From:$senderName < ".$senderEmail.">“);

  22. Marilena says:

    hi! thanks for this great form! it’s very simple! i also found a solution reading comments here:
    i just need one (i hope simple) thing: when i roll over “send” and “reset” button i can’t see hand cursor. i tried
    sendBtn.buttonMode = true;
    and
    sendBtn.buttonMode = true; (both to send and reset)
    but i always see default cursor.
    can you help me showing hand cursor? thanks in advance
    :D

  23. Paul Ferrie says:

    The zip file has been updated with relevent code for default mouse over button.

  24. Marilena says:

    oh that’s great! thank you so much! ^_^

  25. Phil says:

    The form works perfect. Question, the email footer displays the date and time but I believed it is currently based on the server’s date and time. How do I change it to the sender’s date and time?

  26. Paul says:

    Hi Phil,

    You code use flash’s date object and pass that to the php script.
    This will pass the date/time of the system instead of the server.

  27. Art says:

    Hi,

    I’m having a problem, maybe you could give me some direction to where’s the error.
    The form do work when I test it from Flash, but it wont work when running from the server.

    Any idea?

  28. Paul says:

    Can you send me a link to where you have it running on a server.

  29. Josie says:

    Hi Paul! thanks for your link! It’s been REALLY helpful!!!
    I’m having some trouble though… Even though I receive the emails (which is AMAZING!) the error message (and no “message sent” message) still shows…. I’m barely learning flash, not to mention php… but it seems as though the php isn’t “echoing”.
    Any insight of why this would be happening?

    Thanks in advance,

    Josie
    (following you from Argentina!!!)

  30. Paul says:

    Can you email me a link to the php file on your server and then i can point to it and test from my end.

  31. Josie says:

    just sent you the email at djdomain… hope you can help!

  32. Josie says:

    I thought it was that but discarded it since I do receive the message… Guess I’ll just remove the error message in the flash file… and hope it works everytime! :)

    thanks for your help!

  33. Demetrius McClain says:

    I’m not sure if you can help me with this but I’ve googled all forums and tutorials and even the author of the original script and to no avail. I have customized this AS3 form that is processed with php. I need to get the phone number to send. As it stands only the name and email are being received in the email. The title phone is being posted but the value is empty. The following code is what I am dealing with. If you want the actual files just send me an email and I will forward them to you. Thanks in advance for any assistance you may be able to provide.

    contact.php

    ContactMain.as
    package
    {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.FocusEvent;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.text.TextField;
    import flash.utils.Timer;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLRequestMethod;
    import flash.net.URLLoaderDataFormat;

    /**
    * ContactForm Version 1
    *
    * @author activetofocus team
    * @since 2010-02-27
    */
    public class ContactMain extends MovieClip
    {
    //————————————–
    // Variables
    //————————————–
    private var timer:Timer;
    private var urlload:URLLoader;
    private var urlrequest:URLRequest;

    /**
    * constructor
    */
    public function ContactMain()
    {

    }

    //————————————–
    // Function
    //————————————–

    /**
    * data init
    */
    public function init():void
    {
    state_mc.visible = false;

    clear_btn.buttonMode = true;
    clear_btn.addEventListener(MouseEvent.CLICK,onBtnMouseClickEvent);
    clear_btn.addEventListener(MouseEvent.MOUSE_OVER,onBtnMouseOverEvent);
    clear_btn.addEventListener(MouseEvent.MOUSE_OUT,onBtnMouseOutEvent);

    send_btn.buttonMode = true;
    send_btn.addEventListener(MouseEvent.CLICK,onBtnMouseClickEvent);
    send_btn.addEventListener(MouseEvent.MOUSE_OVER,onBtnMouseOverEvent);
    send_btn.addEventListener(MouseEvent.MOUSE_OUT,onBtnMouseOutEvent);

    name_txt.addEventListener(FocusEvent.FOCUS_IN,onTextFocusInEvent);
    name_txt.addEventListener(FocusEvent.FOCUS_OUT,onTextFocusOutEvent);

    mail_txt.addEventListener(FocusEvent.FOCUS_IN,onTextFocusInEvent);
    mail_txt.addEventListener(FocusEvent.FOCUS_OUT,onTextFocusOutEvent);

    phone_txt.addEventListener(FocusEvent.FOCUS_IN,onTextFocusInEvent);
    phone_txt.addEventListener(FocusEvent.FOCUS_OUT,onTextFocusOutEvent);

    }

    /**
    * check name textfield
    */
    private function checkNameText():void
    {
    if(name_txt.text == “”)
    {
    //name_error.visible = true;
    name_txt.text = “Please enter your name!”;
    }
    else
    {
    //name_right.visible = true;
    }
    }

    /**
    * check mail textfield
    */
    private function checkMailText():void
    {
    if(mail_txt.text == “”)
    {
    //mail_error.visible = true;
    mail_txt.text = “Please enter your email!”;
    }
    else
    if(!authenticationEmail(mail_txt.text))
    {
    //mail_error.visible = true;
    mail_txt.text = “Please enter a valid email!”;
    }
    else
    {
    //mail_right.visible = true;
    }
    }

    /**
    * check phone text field
    */
    private function checkphoneText():void
    {
    if(phone_txt.text == “”)
    {
    //phone_error.visible = true;
    phone_txt.text = “Please enter phone number!”;
    }
    else
    {

    }
    }

    /**
    * check message textfield
    */

    /**
    * set click state
    */
    private function setClickState(bool:Boolean = false):void
    {
    name_txt.mouseEnabled = bool;
    mail_txt.mouseEnabled = bool;
    phone_txt.mouseEnabled = bool;
    clear_btn.mouseEnabled = bool;
    send_btn.mouseEnabled = bool;
    }

    /**
    * authentication email format
    */
    private function authenticationEmail(str:String):Boolean
    {
    var p:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
    var r:Object = p.exec(str);
    if( r == null )
    {
    return false;
    }
    return true;
    }

    //————————————–
    // Event
    //————————————–

    /**
    * clear,send button mouse click event
    */
    private function onBtnMouseClickEvent(evt:MouseEvent):void
    {
    if(state_mc.visible) state_mc.visible = false;
    if(evt.currentTarget == clear_btn)
    {
    name_txt.text = “”;
    mail_txt.text = “”;
    phone_txt.text = “”;

    }
    else
    {

    if(name_txt.text !=”" && mail_txt.text != “” && phone_txt.text != “”)
    {
    if(authenticationEmail(mail_txt.text))
    {
    setClickState();

    state_mc.gotoAndStop(1);
    state_mc.visible = true;

    urlload = new URLLoader;
    urlrequest = new URLRequest( “contact.php” );
    urlrequest.method = URLRequestMethod.POST;

    var dataStr:String = “name=” + name_txt.text
    + “&email=” + mail_txt.text
    + “&phone=” + phone_txt.text;

    var urlVar:URLVariables = new URLVariables(dataStr);
    urlVar.dataFormat = URLLoaderDataFormat.TEXT;

    urlrequest.data = urlVar;

    urlload.load(urlrequest);
    urlload.addEventListener(Event.COMPLETE,onLoadCompleteEvent);

    timer = new Timer(2500,1);
    timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerCompleteEvent);
    timer.start();
    }
    else
    {
    checkMailText();
    }
    }
    else
    {
    checkNameText();
    checkMailText();
    checkphoneText();

    }
    }
    }

    /**
    * clear,send button mouse over event
    */
    private function onBtnMouseOverEvent(evt:MouseEvent):void
    {
    evt.currentTarget.gotoAndStop(2);
    }

    /**
    * clear,send button mouse uut event
    */
    private function onBtnMouseOutEvent(evt:MouseEvent):void
    {
    evt.currentTarget.gotoAndStop(1);
    }

    /**
    * textField get focus event
    */
    private function onTextFocusInEvent(evt:FocusEvent):void
    {
    if(state_mc.visible) state_mc.visible = false;
    if(evt.currentTarget == name_txt)
    {
    if(name_txt.text == “Please enter your name!”) name_txt.text = “”;
    name_mc.gotoAndStop(2);
    }
    else
    if(evt.currentTarget == mail_txt)
    {
    if(mail_txt.text == “Please enter your email!” || mail_txt.text == “Please enter a valid email!”) mail_txt.text = “”;
    mail_mc.gotoAndStop(2);
    }
    else
    if(evt.currentTarget == phone_txt)
    {
    if(phone_txt.text == “Please enter phone number!”) phone_txt.text = “”;
    phone_mc.gotoAndStop(2);
    }

    }

    /**
    * textField focus out event
    */
    private function onTextFocusOutEvent(evt:FocusEvent):void
    {
    if(evt.currentTarget == name_txt)
    {
    name_mc.gotoAndStop(1);
    checkNameText();
    }
    else
    if(evt.currentTarget == mail_txt)
    {
    mail_mc.gotoAndStop(1);
    checkMailText();
    }
    else
    if(evt.currentTarget == phone_txt)
    {
    phone_mc.gotoAndStop(1);
    checkphoneText();
    }

    }

    /**
    * send complete event
    */
    private function onLoadCompleteEvent(evt:Event):void
    {
    timer.removeEventListener(TimerEvent.TIMER_COMPLETE,onTimerCompleteEvent);
    timer.stop();

    var backInfo = new URLVariables(evt.currentTarget.data).success;

    if(backInfo == “yes” )
    {
    state_mc.gotoAndStop(3);

    name_txt.text = “”;
    mail_txt.text = “”;
    phone_txt.text = “”;

    }
    else
    {
    state_mc.gotoAndStop(2);
    }
    setClickState(true);
    }

    /**
    * timer complete event
    */
    private function onTimerCompleteEvent(evt:TimerEvent):void
    {
    urlload.removeEventListener(Event.COMPLETE,onLoadCompleteEvent);
    timer.removeEventListener(TimerEvent.TIMER_COMPLETE,onTimerCompleteEvent);
    setClickState(true);
    state_mc.gotoAndStop(2);
    }
    }
    }

  34. Nick Seego says:

    Hey paul,
    thanks u so much…..everything works really good……….say I was wondering though ….How do you put in additional emails in the php file?

  35. Paul says:

    All you need to do is add second email address separated by a comma in the php script..