Contact Forms Using AS2/AS3 With PHP

Posted by mike@sd-dezign on Sep 20 2007 | AS2, AS3, Flash, PHP

UPDATE : The files for this have been moved to my main site now.  You can find them here

Per request on the Adobe Actionscript Forums, this is the base code I have used for a contact form for some time now.  There are a few things that need to be changed such as $toaddress in the PHP file (should be set to your e-mail address) and the mail.php call in both version of the Flash FLA need to be updated with the full path to the mail.php file after it has been uploaded to your server.  Click Read More for the code.

 The PHP File (mail.php)

<?php

/* mail.php - version 1.0 */
/* Basic form mailer script */
/* creator - Mike Garcia II */

/* Pull POST data from Flash */
$name=$_POST[’name’];
$email=$_POST[’email’];
$message=$_POST[’message’];

/* Title of the E-mail */
$subject = ‘Mail Form Title’;

/* The e-mail address we’ll send the data to. */
$toaddress=’email@somedomain.com’;

/* This is not general practice.
   Typically you’ll want to error check all data being sent from
   Flash. All we’re doing here is simple formatting.
*/
$name=trim($name);
$email=trim($email);
$subject=stripslashes($subject);
$message=stripslashes($message);

/* Send our e-mail
   If mail() succeeds, respond with success message
   If mail() fails, send with failure message
*/
if(mail($toaddress,$subject,$message,’From: ‘.$name.’ <’.$email.’>’)){
 echo ‘response=passed’;
} else {
 echo ‘response=failed’;

?>

  

 The Flash 8 (AS2) Document (as2contact.fla - Download)

// Email LoadVars
var sVars = new LoadVars();
var rVars = new LoadVars();

rVars.onLoad = function() {
 
 if(this.response == “passed”){
  eName.text = “”;
  eEmail.text = “”;
  eComments.text = “”;
  
  message.text = “”;
 }
}

submit.onRelease = function(){
 
 // Set up sender
 sVars.eName  = eName.text;
 sVars.eEmail = eEmail.text;
 sVars.eComm  = eComments.text;
 
 trace(”Sending: ” + sVars.toString());
 
 message.text = “Sending e-mail…”;
 
 sVars.sendAndLoad(”mail.php”, rVars, “POST”);
}

   

The Flash CS3 (AS3) Document - (as3contact.fla - Download)

submitBTN.buttonMode = true;
submitBTN.addEventListener( MouseEvent.CLICK, submitClick );
submitBTN.addEventListener( MouseEvent.MOUSE_OVER, submitOver );
submitBTN.addEventListener( MouseEvent.MOUSE_OUT, submitOut );

function submitClick( e:MouseEvent ):void {
 // Send the form
 sendData();
}

function submitOver( e:MouseEvent ):void {
 // Go to roll-over state for the button
 submitBTN.gotoAndStop(2);
}

function submitOut( e:MouseEvent ):void {
 // Go to the normal state for the button
 submitBTN.gotoAndStop(1);
}

function sendData():void {
 var variables:URLVariables = new URLVariables();
 variables.custname = nameBox.text;
 variables.cusemail = emailBox.text;
 variables.custbody = commentBox.text;
 variables.sendresponse = “SENDING”
 
 msgBox.text = variables.sendresponse;
 
 var _request:URLRequest = new URLRequest( “mail.php?cacheKiller=” + (new Date()).getTime());
 _request.data = variables;
 _request.method = URLRequestMethod.POST;
 
 var loader:URLLoader = new URLLoader();
 loader.dataFormat = URLLoaderDataFormat.VARIABLES;
 loader.addEventListener( Event.COMPLETE, sendComplete );
 loader.addEventListener( IOErrorEvent.IO_ERROR, sendIOError );
 loader.load( _request );
}

function sendComplete( e:Event ):void {
 msgBox.text = “E-mail Sent”;
 clearForm();
 
 var tmr:Timer = new Timer( 5000, 1 );
 tmr.addEventListener( TimerEvent.TIMER, clearMessage );
 tmr.start();
}

function sendIOError( e:IOErrorEvent ):void {
 msgBox.text = “Error sending e-mail”;
 
 var tmr:Timer = new Timer( 5000, 1 );
 tmr.addEventListener( TimerEvent.TIMER, clearMessage );
 tmr.start();
}

function clearForm( ):void {
 nameBox.text = “”;
 emailBox.text = “”;
 commentBox.text = “”;
}

function clearMessage( e:Event ):void {
 msgBox.text = “”;
}

13 comments for now

13 Responses to “Contact Forms Using AS2/AS3 With PHP”

  1. Nate

    Hi Mike,

    Thank you VERY much for posting this. All of the mailer scripts I was using were AS1, and didn’t even begin to work in CS3. However, I’m afraid -using CS3 (AS2) - that this doesn’t work. I’ve loaded your example in flash player 9 and…nothing. The email & path to php are fine.. so i’m not sure where I’m going wrong. Any thoughts? Thank you.

    08 Oct 2007 at 3:22 am

  2. Nate

    Oops. I figured it out. Your variable names don’t match up b/t flash & the php script. Thanks!

    08 Oct 2007 at 3:59 am

  3. Can you please post the .fla files again?

    01 Nov 2007 at 4:10 am

  4. mike@sd-dezign

    Lars,

    I’m in the process of updating my entire site and the general overall direction I’ll be taking with it in the future. As such, I removed all content for these files until the updates were complete. You should be able to find all of this information in the new Tutorials sections under the community forums early next week.

    02 Nov 2007 at 3:51 pm

  5. any news about the .fla files? Are they back on the net?

    09 Nov 2007 at 3:12 pm

  6. mike@sd-dezign

    Hans, send me an e-mail directly and I’ll send you the files. I’m in the process of updating the old tutorials and AS etc. If you want the old files, just e-mail me and I’ll get them to you.

    12 Nov 2007 at 2:08 pm

  7. Mike Treagust

    Hey Mike,

    Any chance you could email as3contact.fla through to me as well. Would be much appreciated.

    Thanks

    Mike

    26 Nov 2007 at 2:59 pm

  8. Anabel

    Hey Mike,

    Could you email the ac3contact.fla too? Thanks for the help. Great work!

    -Anabel

    29 Nov 2007 at 3:39 pm

  9. Carlos M

    Hey Mike, could you send me the files for the AS3 form.
    It would be greatly appreciated.
    Carlos

    09 Dec 2007 at 6:21 pm

  10. Christy

    Would I also be able to get a copy for the AS3 form? Thanks for your help!

    11 Dec 2007 at 8:03 pm

  11. mehdi

    Hello, would that be possible to have the AS3 + php contact form please ? I’m still on as2 and learning as3 but that’s a bit tricky for me.

    Thanks in advance.

    Mehdi

    14 Dec 2007 at 10:11 am

  12. Hey Mike…

    the as3 contact form…I feel like I’m standing in line here…could I look at the source as well?

    how is texas? I’m still in chicago…and its cold!!!

    14 Dec 2007 at 5:00 pm

  13. Jochem

    Hi,
    as far as i know, in the phpfile the stringvariables aint the same as in the flashfile.
    plus in the flashcode you need to change:

    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    to
    loader.dataFormat = URLLoaderDataFormat.TEXT;

    (not that it is working rightnow.. but as least flash aint giving errors anymore)

    09 Jan 2008 at 6:04 am

Trackback URI | Comments RSS

Leave a Reply