Flash image upload contact form - Php code confusing!!
Posted: Sat Dec 12, 2009 8:59 am
Hello! I've got a Flash contact form (as3) which I've written php code for. I need this code to send all the form data PLUS image attachment to my email but it's not doing either for some reason. There's four seperate scripts for this:
Flash Image Upload Script:
Flash Content Data Upload:
'Send Email' PHP code:
Uploader_Script PHP code:
Flash Image Upload Script:
Code: Select all
// First thing is to set the flashing upload message clip to invisible
uploadMsg.visible = false;
// Set the URL for the PHP uploader script
var URLrequest:URLRequest = new URLRequest("http://www.tidydays.co.uk/uploader_script.php");
// Assign the image types Filter
var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
// Assign the document types filter
var textTypes:FileFilter = new FileFilter("Text Files (*.txt, *.rtf)", "*.txt; *.rtf");
// Add both filter types to an array
var allTypes:Array = new Array(imageTypes, textTypes);
// Set the FileReference name
var fileRef:FileReference = new FileReference();
// Add event listeners for its various fileRef functions below
fileRef.addEventListener(Event.SELECT, syncVariables);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
// Add event listeners for your 2 buttons
browse_btn.addEventListener(MouseEvent.CLICK, browseBox);
upload_btn.addEventListener(MouseEvent.CLICK, uploadVars);
// Function that fires off when the user presses "browse for a file"
function browseBox(event:MouseEvent):void {
fileRef.browse(allTypes);
}
// Function that fires off when the user presses the "upload it now" btn
function uploadVars(event:MouseEvent):void {
uploadMsg.visible = true;
fileRef.upload(URLrequest);
upload_btn.visible = false;
}
// Function that fires off when File is selected from PC and Browse dialogue box closes
function syncVariables(event:Event):void {
fileDisplay_txt.text = "" + fileRef.name;
blocker.visible = false;
upload_btn.visible = true;
progressBar.width = 2;
var variables:URLVariables = new URLVariables();
variables.todayDate = new Date();
variables.Name = "Dude"; // This could be an input field variable like in my contact form tutorial : )
variables.Email = "info@tidydays.co.uk"; // This one the same
URLrequest.method = URLRequestMethod.POST;
URLrequest.data = variables;
}
// Function that fires off when upload is complete
function completeHandler(event:Event):void {
uploadMsg.visible = false;
blocker.visible = true;
status_txt.text = fileRef.name + " has been uploaded.";
fileDisplay_txt.text = "";
}
// Function that fires off when the upload progress begins
function progressHandler(event:ProgressEvent):void {
// we want our progress bar to be 200 pixels wide when done growing so we use 200*
// Set any width using that number, and the bar will be limited to that when done growing
progressBar.width = Math.ceil(200*(event.bytesLoaded/event.bytesTotal));
}Code: Select all
contact_name.text = contact_age.text = contact_dob.text = contact_address.text =
contact_tel.text = contact_email.text = contact_course.text = contact_experience.text =
contact_extraInfo.text = "";
send_button.addEventListener(MouseEvent.CLICK, submit);
reset_button.addEventListener(MouseEvent.CLICK, reset);
var timer:Timer;
var var_load:URLLoader = new URLLoader;
var URL_request:URLRequest = new URLRequest( "send_email.php" );
URL_request.method = URLRequestMethod.POST;
function submit(e:MouseEvent):void
{
if( contact_name.text == "" || contact_age.text == "" ||
contact_dob.text == "" || contact_address.text == "" ||
contact_tel.text == "" ||contact_email.text == "" ||
contact_course.text == "" ||contact_experience.text == "" ||
contact_extraInfo.text == "" )
{
message_status.text = "Please fill up all text fields.";
}
else if( !validate_email(contact_email.text) )
{
message_status.text = "Please enter the valid email address.";
}
else
{
message_status.text = "sending...";
var email_data:String = "name=" + contact_name.text
+ "&age=" + contact_age.text
+ "&dob=" + contact_dob.text
+ "&address=" + contact_address.text
+ "&tel=" + contact_tel.text
+ "&email=" + contact_email.text
+ "&course=" + contact_course.text
+ "&experience=" + contact_experience.text
+ "&extraInfo=" + contact_extraInfo.text
var URL_vars:URLVariables = new URLVariables(email_data);
URL_vars.dataFormat = URLLoaderDataFormat.TEXT;
URL_request.data = URL_vars;
var_load.load( URL_request );
var_load.addEventListener(Event.COMPLETE, receive_response );
}
}
function reset(e:MouseEvent):void
{
contact_name.text = contact_age.text = contact_dob.text = contact_address.text =
contact_tel.text = contact_email.text = contact_course.text = contact_experience.text =
contact_extraInfo.text = "";
}
function validate_email(s:String):Boolean
{
var p:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
var r:Object = p.exec(s);
if( r == null )
{
return false;
}
return true;
}
function receive_response(e:Event):void
{
var loader:URLLoader = URLLoader(e.target);
var email_status = new URLVariables(loader.data).success;
if( email_status == "yes" )
{
message_status.text = "Success! Your message was sent.";
timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, on_timer);
timer.start();
}
else
{
message_status.text = "Failed! Your message cannot sent.";
}
}
function on_timer(te:TimerEvent):void
{
if( timer.currentCount >= 5 )
{
contact_name.text = contact_age.text = contact_dob.text = contact_address.text =
contact_tel.text = contact_email.text = contact_course.text = contact_experience.text =
contact_extraInfo.text = "";
timer.removeEventListener(TimerEvent.TIMER, on_timer);
}
}'Send Email' PHP code:
Code: Select all
<?php
$contact_name = $_POST['name'];
$contact_age = $_POST['age'];
$contact_dob = $_POST['dob'];
$contact_address = $_POST['address'];
$contact_tel = $_POST['tel'];
$contact_email = $_POST['email'];
$contact_course = $_POST['course'];
$contact_experience = $_POST['experience'];
$contact_extraInfo = $_POST['extraInfo'];
if( $contact_name == true )
{
$sender = $contact_email;
$receiver = "info@tidydays.co.uk";
$client_ip = $_SERVER['REMOTE_ADDR'];
$email_body = "Name: $contact_name \nEmail: $sender \n\nSubject: $contact_email \n\nAge: $contact_age \n\nDob: $contact_dob \n\nAddress: \n\n$contact_address \n\nIP: $client_ip \n\nCourse: $contact_course
\n\nExperience: $contact_experience \n\nExtraInfo: $contact_extraInfo \n\nTidy Days";
$extra = "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();
if( mail( $receiver, "Tidy Days Submission - $contact_subject", $email_body, $extra ) )
{
echo "success=yes";
}
else
{
echo "success=no";
}
}
?>Code: Select all
<?php
// Set local PHP vars from the POST vars sent from flash
$todayDate = $_POST['todayDate'];
$Name = $_POST['Tidy Days'];
$Email = $_POST['info@tidydays.co.uk'];
$filename = $_FILES['Filedata']['name'];
$filetmpname = $_FILES['Filedata']['tmp_name'];
$fileType = $_FILES["Filedata"]["type"];
$fileSizeMB = ($_FILES["Filedata"]["size"] / 1024 / 1000);
// Place file on server, into the images folder
move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$filename);
// This section edits your log file, if you don't need a text log file just delete these lines
$myFile = "logFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "\n\ntodayDate: $todayDate \n Name: $Name \n Email: $Email \n ssid: $ssid \n FileName: $filename \n TmpName: $filetmpname \n Type: $fileType \n Size: $fileSizeMB MegaBytes";
fwrite($fh, $stringData);
fclose($fh);
// End log file edit
?>