• Coding
  • [AS3/PHP] Flash Contact Form

Ok I'm trying to get this contact form working but it just doesn't want to... Here's my code:

Actionscript 3.0 Code:
package 
{
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import XMLManager
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import fl.data.DataProvider;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequestMethod;
	
	public class Contact extends MovieClip
	{
		private var manager:XMLManager;
		private var xmlData:XML;
		private var phpReq:URLRequest;
		private var ldr:URLLoader;
		private var variables:URLVariables;
		
		public function Contact():void
		{
			if (stage)
			{
				init();
			}
			else {
				addEventListener(Event.ADDED_TO_STAGE, init);
			}
		}
		
		private function init():void
		{
			populateCounList();
			initVariables();
		}
		
		private function initVariables():void
		{
			phpReq = new URLRequest("contact.php");
			ldr = new URLLoader();
			variables = new URLVariables();
			ldr.dataFormat = URLLoaderDataFormat.VARIABLES;
			phpReq.method = URLRequestMethod.POST;
			
			name_txt.tabIndex = 0;
			email_txt.tabIndex = 1;
			phone_txt.tabIndex = 2;
			countryList.tabIndex = 3;
			msg_txt.tabIndex = 4;
			status.tabIndex = 5;
			reset.tabIndex = 6;
			submit.addEventListener(MouseEvent.CLICK, submitClicked);
			reset.addEventListener(MouseEvent.CLICK, resetClicked);
		}
		
		private function populateCounList():void
		{
			manager = new XMLManager("countries.xml");
			manager.addEventListener(Event.COMPLETE, dataReady);
		}
			
		private function dataReady(e:Event):void
		{
			var m:XMLManager = e.target as XMLManager;
			xmlData = m.data;
			var dp:DataProvider = new DataProvider();
			var list:XMLList = new XMLList(xmlData.country);
			for (var i:uint = 0; i < list.length(); i++)
			{
				dp.addItem( { label: list[i] } );
			}
			countryList.dataProvider = dp;
		}
		
		private function submitClicked(e:MouseEvent):void
		{	
			var phone:String;
			var country:String;
			
			if (name_txt.text.length <= 0)
			{
				status.text = "** Name Required **";
			}
			else if (!isValidEmail(email_txt.text))
			{
				status.text = "** Email Missing/Invalid Email **";
			}
			else if (msg_txt.text.length <= 0)
			{
				status.text = "** Message Required **";
			}
			else 
			{	
				if (phone_txt.text.length > 0)
				{
					phone = phone_txt.text;
				} else {
					phone = "No Phone";
				}
				if (countryList.selectedIndex != 0)
				{
					country = countryList.selectedLabel
				} else {
					country = "No Location";
				}
				
				variables.senderName = name_txt.text;
				variables.senderEmail = email_txt.text;
				variables.senderPhone = phone;
				variables.senderCountry = country;
				variables.senderMessage = msg_txt.text;
				phpReq.data = variables;
				ldr.load(phpReq);
				ldr.addEventListener(Event.COMPLETE, receiveData);
				status.text = "Processing, please wait...";
			}
		}
		
		private function receiveData(e:Event):void
		{
			if (e.target.data.returnMsg == 1)
			{
				status.text = "Message Sent Successfully";
				name_txt.text = "";
				email_txt.text = "";
				msg_txt.text = "";
				phone_txt.text = "";
				countryList.selectedIndex = 0;
			}
			else {
				status.text = "There was a problem, please try again";
			}
		}
			
		private function resetClicked(e:MouseEvent):void
		{
			name_txt.text = "";
			email_txt.text = "";
			msg_txt.text = "";
			phone_txt.text = "";
			countryList.selectedIndex = 0;
			status.text = "";
		}
			
		private function isValidEmail(email:String):Boolean 
		{
			var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
			return emailExpression.test(email);
		}
	}
}
PHP Code:
<?php

if(empty($_POST['senderEmail']))
{
	echo "Email is missing";
	exit;	
} 

$name = $_POST['senderName'];
$email = $_POST['senderEmail'];
$country = $_POST['senderCountry'];
$phone = $_POST['senderPhone'];
$msg = nl2br($POST['senderMessage']);
$sitename = "www.prodouble-d.com";

$to = "contact@prodouble-d.com";
$date = date("m/d/Y H:i:s");
$subject = "Email From $name via $sitename";
$emailBody = "A visitor to $sitename has left the following information<br />
Sent By: $name <br />
Email: $email <br />
Phone: $phone <br />
<br /><br />
Message Sent:
<br />$msg<br /><br />";  
$emailFooter	= "<br />Sent On: $date<br />Location:$country<br />";
$message = $emailBody.$emailFooter;
$headers  = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From:$name <".$email.">";

$ok = mail($to, $subject, $message, $headers);

if($ok){
	echo "returnMsg=1";
}else{
	echo "returnMsg=0";
}

?>
The status text field says "Processing, please wait..." and just gets stuck there... What am I doing wrong here?
Did you assert that your PHP script works as intended?

You can use tools like Fiddler.

It is quite possible the mail(...) is timing out.
Make sure you're testing in a web server, not in the file system. Other things seem ok, perhaps you should do a test that works for sure, and start from there.
hmmm... I tested the php script and this is what I've got:
Warning: mail() has been disabled for security reasons in /home/kassem/public_html/contact/contact.php on line 33
returnMsg=0
How can I re-enable the mail() function? Do I need to contact my web host or what?

EDIT: I've just contacted my web host and it seems like they're moving to new servers so they disabled some functions including mail(), phpinfo() and others... Lucky for me lol
Shouldn't stop you from testing the code. Just make it blindly succeed.
arithma wroteShouldn't stop you from testing the code. Just make it blindly succeed.
Reminds me of a thedailywtf.com entry where someone saw function with the following return:
return true && true && true
Entry had the text "That's some real truth!"