...

View Full Version : Passing Form Variables



ffsja
02-08-2007, 09:24 PM
Hi everyone,

I'm working on a form that utilizes javascript/ajax/php to send user-submitted files to an email address. The functions mostly seem to work on the general form data. But when I try to process the uploaded files through JavaScript/AJAX it's not sending the email. Here's the page for the actual form where you can upload files:

http://distance.uaf.edu/captcha_test.php

Here's the link to the JavaScript to which the form results are initially sent:

http://distance.uaf.edu/ajax_captcha.js

And finally, here is the code from the actual PHP file that receives the variables and sends the email:


<?php
//Continue the session
session_start();

//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER["REQUEST_METHOD"] <> "POST")
die("You can only reach this page by posting from the html form");

//Check if the security code and the session value are not blank
//and if the input text matches the stored text
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) &&
(!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {

setcookie("firstname",$_REQUEST['firstname'],time()+7776000);
setcookie("middleinitial",$_REQUEST['middleinitial'],time()+7776000);
setcookie("lastname",$_REQUEST['lastname'],time()+7776000);
setcookie("email",$_REQUEST['email'],time()+7776000);
setcookie("address1",$_REQUEST['address1'],time()+7776000);
setcookie("address2",$_REQUEST['address2'],time()+7776000);
setcookie("city",$_REQUEST['city'],time()+7776000);
setcookie("state",$_REQUEST['state'],time()+7776000);
setcookie("zip1",$_REQUEST['zip1'],time()+7776000);
setcookie("zip2",$_REQUEST['zip2'],time()+7776000);
setcookie("phone1",$_REQUEST['phone1'],time()+7776000);
setcookie("phone2",$_REQUEST['phone2'],time()+7776000);
setcookie("phone3",$_REQUEST['phone3'],time()+7776000);
setcookie("for",$_REQUEST['for'],time()+7776000);
setcookie("lessonnum",$_REQUEST['lessonnum'],time()+7776000);
setcookie("month",$_REQUEST['month'],time()+7776000);
setcookie("day",$_REQUEST['day'],time()+7776000);
setcookie("year",$_REQUEST['year'],time()+7776000);
setcookie("course",$_REQUEST['course'],time()+7776000);
setcookie("coursename",$_REQUEST['coursename'],time()+7776000);
setcookie("instructor",$_REQUEST['instructor'],time()+7776000);

// Read POST request params into global vars
$to = "ffsja@uaf.edu";
$subject = "lessons submitted";
$firstname = $_REQUEST['firstname'];
$middleinitial = $_REQUEST['middleinitial'];
$lastname = $_REQUEST['lastname'];
$term = $_REQUEST['term'];
$address1 = $_REQUEST['address1'];
$address2 = $_REQUEST['address2'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip1 = $_REQUEST['zip1'];
$zip2 = $_REQUEST['zip2'];
$lessonnum = $_REQUEST['lessonnum'];
$phone1 = $_REQUEST['phone1'];
$phone2 = $_REQUEST['phone2'];
$phone3 = $_REQUEST['phone3'];
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$year = $_REQUEST['year'];
$coursename = $_REQUEST['coursename'];
$instructor = $_REQUEST['instructor'];
$course = $_REQUEST['course'];
$section = $_REQUEST['section'];
$for = $_REQUEST['for'];
$message = "<i>You've received a student lesson.</i> <br /><br /><b>Full Name:</b> " . $firstname . " " . $middleinitial . " " . $lastname . "<br /><b>Address:</b> " . $address1 . " " . $address2 . "<br /><b>City:</b> " . $city . "<br /><b>State:</b> " . $state . "<br /><b>Zip:</b> " . $zip1 . "-" . $zip2 . "<br /><b>Phone:</b> " . "(" . $phone1 . ") " . $phone2 . "-" . $phone3 . "<br /><b>Course Name:</b> " . $coursename . "<br /><b>Course:</b> " . $course . "<br /><b>Term:</b> " . $term . "<br /><b>Instructor:</b> " . $instructor . "<br /><b>Lesson Number:</b> " . $lessonnum . "<br /><b>Expected Completion Date:</b> " . $day . "/" . $month . "/" . $year . "<br /><b>For:</b> " . $for;

// Obtain file upload vars
$fileatt = $_FILES['userfile1']['tmp_name'];
$fileatt_type = $_FILES['userfile1']['type'];
$fileatt_name = $_FILES['userfile1']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

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

}

if ($ok) {
echo "<p>Lessons Submitted! Thank you.</p>";
} else {
echo "<p>Lessons could not be sent. Sorry!</p>";
}

} else {
echo "<h1>Test failed! Try again!</h1>";
}
?>

Thanks everyone!
sage

neomaximus2k
02-08-2007, 09:38 PM
does it send an email out at all?

ffsja
02-08-2007, 11:03 PM
No, it doesn't. When I remove the reference to the file attachment variable - then it emails ok.

neomaximus2k
02-08-2007, 11:11 PM
No, it doesn't. When I remove the reference to the file attachment variable - then it emails ok.

Right well I had a similar problem when attaching files to email. It turned out to be a permission error to the file (had to crawl through an hours worth of error ogs, and this was at bigpockets.co.uk who get 50,000 visitors a week so you can imagine how anoyed i was) so I used the copy() function to move it to a folder that was chmodded to 777.

I have added the changes to your script, not tested but beleive it will work


<?php
//Continue the session
session_start();

//Make sure that the input come from a posted form. Otherwise quit immediately
if ($_SERVER["REQUEST_METHOD"] <> "POST")
die("You can only reach this page by posting from the html form");

//Check if the security code and the session value are not blank
//and if the input text matches the stored text
if ( ($_REQUEST["txtCaptcha"] == $_SESSION["security_code"]) &&
(!empty($_REQUEST["txtCaptcha"]) && !empty($_SESSION["security_code"])) ) {

setcookie("firstname",$_REQUEST['firstname'],time()+7776000);
setcookie("middleinitial",$_REQUEST['middleinitial'],time()+7776000);
setcookie("lastname",$_REQUEST['lastname'],time()+7776000);
setcookie("email",$_REQUEST['email'],time()+7776000);
setcookie("address1",$_REQUEST['address1'],time()+7776000);
setcookie("address2",$_REQUEST['address2'],time()+7776000);
setcookie("city",$_REQUEST['city'],time()+7776000);
setcookie("state",$_REQUEST['state'],time()+7776000);
setcookie("zip1",$_REQUEST['zip1'],time()+7776000);
setcookie("zip2",$_REQUEST['zip2'],time()+7776000);
setcookie("phone1",$_REQUEST['phone1'],time()+7776000);
setcookie("phone2",$_REQUEST['phone2'],time()+7776000);
setcookie("phone3",$_REQUEST['phone3'],time()+7776000);
setcookie("for",$_REQUEST['for'],time()+7776000);
setcookie("lessonnum",$_REQUEST['lessonnum'],time()+7776000);
setcookie("month",$_REQUEST['month'],time()+7776000);
setcookie("day",$_REQUEST['day'],time()+7776000);
setcookie("year",$_REQUEST['year'],time()+7776000);
setcookie("course",$_REQUEST['course'],time()+7776000);
setcookie("coursename",$_REQUEST['coursename'],time()+7776000);
setcookie("instructor",$_REQUEST['instructor'],time()+7776000);

// Read POST request params into global vars
$to = "ffsja@uaf.edu";
$subject = "lessons submitted";
$firstname = $_REQUEST['firstname'];
$middleinitial = $_REQUEST['middleinitial'];
$lastname = $_REQUEST['lastname'];
$term = $_REQUEST['term'];
$address1 = $_REQUEST['address1'];
$address2 = $_REQUEST['address2'];
$city = $_REQUEST['city'];
$state = $_REQUEST['state'];
$zip1 = $_REQUEST['zip1'];
$zip2 = $_REQUEST['zip2'];
$lessonnum = $_REQUEST['lessonnum'];
$phone1 = $_REQUEST['phone1'];
$phone2 = $_REQUEST['phone2'];
$phone3 = $_REQUEST['phone3'];
$month = $_REQUEST['month'];
$day = $_REQUEST['day'];
$year = $_REQUEST['year'];
$coursename = $_REQUEST['coursename'];
$instructor = $_REQUEST['instructor'];
$course = $_REQUEST['course'];
$section = $_REQUEST['section'];
$for = $_REQUEST['for'];
$message = "<i>You've received a student lesson.</i> <br /><br /><b>Full Name:</b> " . $firstname . " " . $middleinitial . " " . $lastname . "<br /><b>Address:</b> " . $address1 . " " . $address2 . "<br /><b>City:</b> " . $city . "<br /><b>State:</b> " . $state . "<br /><b>Zip:</b> " . $zip1 . "-" . $zip2 . "<br /><b>Phone:</b> " . "(" . $phone1 . ") " . $phone2 . "-" . $phone3 . "<br /><b>Course Name:</b> " . $coursename . "<br /><b>Course:</b> " . $course . "<br /><b>Term:</b> " . $term . "<br /><b>Instructor:</b> " . $instructor . "<br /><b>Lesson Number:</b> " . $lessonnum . "<br /><b>Expected Completion Date:</b> " . $day . "/" . $month . "/" . $year . "<br /><b>For:</b> " . $for;

// Obtain file upload vars
$fileatt = $_FILES['userfile1']['tmp_name'];
$fileatt_type = $_FILES['userfile1']['type'];
$fileatt_name = $_FILES['userfile1']['name'];

$headers = "From: $from";

#### added a few more lines here i tend to copy the image from
#### the temp folder due to permission problems
$newlocation = $_SERVER['DOCUMENT_ROOT']."tempster/".$fileatt_name; ## rember to chmod this folder to 777
$cp = copy($_FILES['image']['tmp_name'], $newlocation)
or die("Unable to move uploaded file");
chmod ($newlocation, octdec(0777));

if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($newlocation,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";

// Base64 encode the file data
$data = chunk_split(base64_encode($data));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

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

}

if ($ok) {
echo "<p>Lessons Submitted! Thank you.</p>";
} else {
echo "<p>Lessons could not be sent. Sorry!</p>";
}

} else {
echo "<h1>Test failed! Try again!</h1>";
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum