PDA

View Full Version : contact form with file upload


cocoonfx
01-23-2011, 10:13 PM
Hi

I have a contact form which works very well. However I wanted to add a file upload so people can attach CV's. I have copied the tutorial for the dynamic file upload. However I have tried to combine the upload script with the contact pages php validation but I can not get the form to work.

Both scripts work separately but not together and I can not work out why not.

Any assistance would be appreciated.

<form id="form1" name="phpformmailer" method="post" action="testsent.php" enctype="multipart/form-data">
<label for="membership">1.Membership Grade </span>(Note 1)</label>
<select name="grade" size="1" id="grade">
<option selected="selected" value="Member">Member</option>
<option value="Associate">Associate</option>
<option value="Graduate">Graduate</option>
<option value="Student">Student</option>
</select>
<label for="personal">2. Personal Details</label>
<input name="Name" type="text" id="Name" size="20" />
<label for="title">Title</label>
<input name="title" type="text" id="title" size="20" />
<label for="e-mail Address">E-mail Address</label>
<input type="text" name="email" id="email" />
<label for="qualifcations">Professional Qualifications</label>
<textarea name="themessage" cols="20" rows="2" id="themessage" themessage="themessage" ></textarea>
<label for="dob">Date of Birth</label>
<input name="dob" type="text" id="dob" size="20" />
<label for="Home address">Home Address</label>
<textarea name="theaddress" cols="20" rows="2" id="theaddress" themessage="theaddress"></textarea>
<label for="home postcode">Home Postcode</label>
<input type="text" name="Postcode" id="Postcode" size="20" />

<label for="company">Company Details</label
><label for"company name">Company Name</label>
<input name="company" type="text" id="company" size="20" />
<label for="company address">Company Address</label>
<textarea name="theaddress2" cols="30" rows="5" class="contentWrap" id="theaddress2" themessage="theaddress2"></textarea>
<label for="postcode">Post Code</label>
<input name="comp_postcode" type="text" id="comp_postcode" size="20" />
<label for="comp-telephone">Telephone</label>
<input name="comp_phone" type="text" id="comp_phone" size="20" />
<label for="fax">Fax</label>
<input name="comp_fax" type="text" id="comp_fax" size="20" />
<label for="email">email</label>
<input name="comp_email" type="text" id="comp_email" size="20" />
<label for="general">3. General Education - Final Level - (Note 3)</label>
<label for="school">School</label>
<textarea name="theschool" cols="30" rows="5" class="contentWrap" id="theschool" themessage="theschool"></textarea>

<label for="grades">Subjects, Grades &amp; Dates</label>
<textarea name="thegrades" cols="20" rows="2" id="thegrades" themessage="thegrades"></textarea>
<label for="university">4. Further Education and Academic/Vocational Qualifications</span> (Note
4) <em>please supply full details including training records or attach [send under separate cover]a CV. Failure to supply adequate information may result in a lower membership status being allocated. </em></label>
<label for="uni">University/College</label>
<textarea name="theuni" cols="20" rows="5" id="theuni" themessage="theuni"></textarea>
<label for="course">Course/Subjects &amp; Year</label>
<textarea name="thecourse" cols="20" rows="2" id="thecourse" themessage="thecourse"></textarea>
<label for="award">Award</label>
<input name="fe_award" type="text" id="fe_award" size="20" />
<label for="professional">5. Professional Body</span> (Note 5)</label>
<label for="body">Body</label>
<input name="probody" type="text" id="probody" size="20" />
<label for="elected">Date Elected</label>
<input name="date_elected" type="text" id="date_elected" size="20" />
<label for="grade">Grade</label>
<input name="pro_grade" type="text" id="pro_grade" size="20" />
<label for="job">6.Previous Job </span>(Note 6)</label>
<label for="job"> Give dates (Start and finish) Name of company and job description</label>
<textarea name="thejob" cols="30" rows="5" id="thejob" themessage="thejob"></textarea>
<label for="previous">6a. Previous Job<strong> </strong></span>(Note 6)</label>
<label for="previous">Give dates (Start and finish) Name of company and job description</label>
<textarea name="thejob2" cols="20" rows="4" id="thejob2" themessage="thejob2"></textarea>

<label for="present">7. Present Position</label>
<label for="company2">Company</label>
<input name="presco" type="text" id="company" size="20" />
<label for="title">Title of Your Position</label>
<input name="prestitle" type="text" id="prestitle" size="20" />
<label for="appointed">Date Appointed</label>
<input name="appoint" type="text" id="appoint" size="20" />
<label for="body">To whom do you report:</label>
<label for="boss">Name</label>
<input name="boss" type="text" id="boss" size="20" />
<label for"bossp">Position</label>
<input name="bosspos" type="text" id="bosspos" size="20" />
<label for="nature">Nature of work and duties/responsibilities</label>
<textarea name="theduties" cols="30" rows="5" class="contentWrap" id="theduties" themessage="theduties"></textarea>

<label for"referee">8. Referee</span> (Note 8)</label>
<label for="name">Name</label>
<input name="ref" type="text" id="ref" size="20" />
<label for="poss"> Position</label>
<input name="refpos" type="text" id="refpos" size="20" />
<label for="member">Member of WMS</label>
<select name="refmem" size="1" id="refmem">
<option selected="selected" value="Yes">Yes</option>
<option value="No">No</option>
</select>
<label for="ref email">Referee e-mail Address</label>
<textarea name="refmail" cols="20" rows="2" id="refmail" themessage="refmail"></textarea>
<label for='uploaded_file'>Select A File To Upload:</label> <br>
<input type="file" name="uploaded_file">
<label for="captcha_iimage">Anti Spam code</label> <img src="captcha_image.php" width="150" height="23" /><br />
<label for="captcha_input">Please enter above code</label>
<input type="text" name="captcha_input" size="12">
<P>Please refer to the membership homepage for Notes </p
><p>Please note that applications are processed more quickly if adequate information is provided at the outset</p>
<p>&nbsp;</p>
<script language="JavaScript">
function validateForm()
{
var okSoFar=true
with (document.phpformmailer)
{
var foundAt = email.value.indexOf("@",0)
if (foundAt < 1 && okSoFar)
{
okSoFar = false
alert ("Please enter a valid email address.")
email.focus()
}
var e1 = email.value
var e2 = email2.value
if (!(e1==e2) && okSoFar)
{
okSoFar = false
alert ("Email addresses you entered do not match. Please re-enter.")
email.focus()
}
if (thesubject.value=="" && okSoFar)
{
okSoFar=false
alert("Please enter the subject.")
thesubject.focus()
}
if (themessage.value=="" && okSoFar)
{
okSoFar=false
alert("Please enter the details for your enquiry.")
themessage.focus()
}
if (thejob.value=="" && okSoFar)
{
okSoFar=false
alert("Please enter the details for your enquiry.")
themessage.focus()
}
if (theduties.value=="" && okSoFar)
{
okSoFar=false
alert("Please enter the details for your enquiry.")
themessage.focus()
}
if (okSoFar==true) submit();
}
}
</script>


<label for="Submit"></label>
<input type="submit" name="Submit" class="buttonSubmit" value="Send" id="Submit" ONCLICK="javascript:validateForm()">
</form>





if ($_SERVER["REQUEST_METHOD"] <> "POST")
die("You can only reach this page by posting from the html form");
session_start();



$target_path = "/home/user/public_html/att/".$_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);


$capt = $_POST["captcha_input"];

$valid_ref1="http://www.domain.org.uk/testform.html";
$valid_ref2="http://www.domain.org.uk/testform.html";
$replyemail="info@.co.uk";
function clean_input_4email($value, $check_all_patterns = true)
{
$patterns[0] = '/content-type:/';
$patterns[1] = '/to:/';
$patterns[2] = '/cc:/';
$patterns[3] = '/bcc:/';
if ($check_all_patterns)
{
$patterns[4] = '/\r/';
$patterns[5] = '/\n/';
$patterns[6] = '/%0a/';
$patterns[7] = '/%0d/';
}

//NOTE: can use str_ireplace as this is case insensitive but only available on PHP version 5.0.
return preg_replace($patterns, "", strtolower($value));

}

$grade = clean_input_4email($_POST["grade"]);
$Name = clean_input_4email($_POST["Name"]);
$title = clean_input_4email($_POST["title"]);
$email = clean_input_4email($_POST["email"]);
$themessage = clean_input_4email($_POST["themessage"], false);
$dob = clean_input_4email($_POST["dob"]);
$theaddress = clean_input_4email($_POST["theaddress"], false);
$Postcode = clean_input_4email($_POST["Postcode"]);
$company = clean_input_4email($_POST["company"]);
$theaddress2 = clean_input_4email($_POST["theaddress2"], false);
$comptele = clean_input_4email($_POST["comp_phone"]);
$compfax = clean_input_4email($_POST["comp_fax"]);
$comppost = clean_input_4email($_POST["comp_postcode"]);
$compemail = clean_input_4email($_POST["comp_email"]);
$School = clean_input_4email($_POST["theschool"], false);
$grades = clean_input_4email($_POST["thegrades"], false);
$university = clean_input_4email($_POST["theuni"], false);
$course = clean_input_4email($_POST["thecourse"], false);
$FEgrade = clean_input_4email($_POST["fe_award"]);
$probody = clean_input_4email($_POST["probody"]);
$election = clean_input_4email($_POST["date_elected"]);
$POGrade = clean_input_4email($_POST["pro_grade"]);
$previousjob = clean_input_4email($_POST["thejob"], false);
$previousjob2 = clean_input_4email($_POST["thejob2"], false);
$company = clean_input_4email($_POST["company"]);
$jobtitle = clean_input_4email($_POST["prestitle"]);
$appoint = clean_input_4email($_POST["appoint"]);
$boss = clean_input_4email($_POST["boss"]);
$bossposs = clean_input_4email($_POST["bosspos"]);
$theduties = clean_input_4email($_POST["theduties"]);
$refname = clean_input_4email($_POST["ref"]);
$refposition = clean_input_4email($_POST["refpos"]);
$refmem = clean_input_4email($_POST["refmem"]);
$refaddress = clean_input_4email($_POST["refmail"], false);


if ($_POST["captcha_input"] != $_SESSION["pass"]) {




$error_msg='<p align="center"><strong>&nbsp;</strong></p>
<p align="center"><strong>SORRY ERROR - NOT SENT</strong>';
echo "<a href='javascript:history.back(1);'>Back to e mail form</a>";


}

elseif($Name && $email && $capt == $_SESSION["pass"]){



$success_sent_msg='<p align="center"><strong>&nbsp;</strong></p>
<p align="center"><strong>Your message has been successfully sent to us<br>
</strong> and we will reply as soon as possible.</p>
<p align="center">A copy of your query has been sent to you.</p>
<p align="center">Thank you for contacting us.</p>
<p align="center">Please <a href="http://www.domain.org.uk/contact.html">click here</a> to return</p>';

$replymessage = "Hello $Name

Thank you for your request for Membership Grade:- $grade .

We will endeavour to reply to you shortly.


Please DO NOT reply to this email.

--------------------------------------------------
Please visit again http://www.domain.org.uk/contact.html
Kind Regards


************************************************** **********************
Disclaimer:
This email transmission is confidential and intended solely for the person(s) or organisation(s) to which it is addressed. If you are not the intended recipient, you must not copy, distribute or disseminate the information, or take any action in reliance of it. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of any organisation or employer. If you have received this message in error do not open any attachment(s), but please notify the sender (above) deleting this message from your system. Please rely on your own virus checker. No responsibility is taken by the sender for any damage rising out of any bug or virus infection.
************************************************** **********************";

// email variable not set - load $valid_ref1 page
if (!isset($_POST['email']))
{
echo "<script language=\"JavaScript\"><!--\n ";
echo "top.location.href = \"$valid_ref1\"; \n// --></script>";
exit;
}

$ref_page=$_SERVER["HTTP_REFERER"];
$valid_referrer=0;
if($ref_page==$valid_ref1) $valid_referrer=1;
elseif($ref_page==$valid_ref2) $valid_referrer=1;
if(!$valid_referrer)
{
echo "<script language=\"JavaScript\"><!--\n alert(\"$error_msg\");\n";
echo "top.location.href = \"$valid_ref1\"; \n// --></script>";
exit;
}

$themessage =
"
New membership

SECTION 1

Membership Grade:- $grade

--------------------------------------------------

SECTION 2 - PERSONAL DETAILS

Title:- $title
Name :- $Name
Home Address:- $theaddress
Postcode:- $Postcode

Personal Email Address:- $email
Date of Birth:- $dob

--------------------------------------------------

PROFESSIONAL QUALIFICATIONS

Professional Qualifications:- $themessage

Company Name:- $company
Company Address:- $theaddress2 \n
Company Postcode:- $comppost

Company Telephone:- $comptele
Company Fax:= $compfax
Company Email:- $compemail

--------------------------------------------------

SECTION 3

School:- $School
Grades:- $grades
University:- $university
Course/Subjects & Year:- $course

--------------------------------------------------

SECTION 4

Award:- $FEgrade

--------------------------------------------------

SECTION 5

Professional Body:- $probody
Date Elected:- $election
Grade:- $POGrade

--------------------------------------------------

SECTION 6

Previous Job (Start and Finish Dates):- $previousjob

--------------------------------------------------

SECTION 6a

Previous Job (Start and Finish Dates):- $previousjob2

--------------------------------------------------

SECTION 7

Present Company:- $company
Job Title:- $jobtitle
Date Appointed:- $appoint
To Whom you report to:- $boss
Position:- $bossposs
Nature of work and duties/responsibilities:- $theduties

--------------------------------------------------
SECTION 8

Referees Name:- $refname
Position:- $refposition
Member of WMS:- $refmem
Referees Address:- $refaddress

--------------------------------------------------

";
mail("$replyemail",
"New membership for $Name",
"$themessage",
"From: $email\nReply-To: $email");
mail("$email",
"New membership for $Name",
"$replymessage",
"From: $replyemail\nReply-To: $replyemail");
echo $success_sent_msg;


}else{

echo $error_msg;

}

edbr
01-24-2011, 06:19 AM
is that target path correct?
if you fancy using a class for this heres a couple of possible links
http://www.finalwebsites.com/forums/ Olaf ledrers calsses
http://www.metalshell.com/source_code/115/PHP_Mail_Class.html

cocoonfx
01-24-2011, 07:52 AM
Thanks Edbr I will check these out, the link is correct as if just use

$target_path = "/home/user/public_html/att/".$_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path);

and a simple form it works and uploads the file to ./att/ folder. However I think the validation is whats stopping it but I can not see how to get the script to work fully.

cocoonfx
01-29-2011, 12:28 PM
I am complete stuck :o(

I have even tried to get email attachment to work but nothing is happening....

any ideas??

cocoonfx
01-30-2011, 03:00 PM
This works really well:

<form name="form1" id="form1" method="post" action="mailer.php?subscribe=go" enctype="multipart/form-data">
<table width="282" border="0" align="center" cellpadding="0" cellspacing="0">
<tr><td width="120"><font class=text><u>Name:</u></font></td><td width="162"><input name="name" type="text" id="name" size="30" /></td></tr>
<tr><td><font class=text><u>Email:</u></font></td><td><input name="email" type="text" id="email" size="30" /></td></tr>
<tr>
<td>&nbsp;</td>
<td><div align="right">
<input type="hidden" name="action" value="Subscribe" />
<input type="submit" value="Subscribe" />
</div></td>
</tr><input type=text name=from><BR>
File Attachment: <input type="file" name="fileatt" /><BR>
<textarea name=message></textarea>
</table>
</form>

And now the PHP.

<?PHP
$email = $_POST['email'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

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

$headers = "From: $from";

if (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/plain; 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";
}

mail ("$email", "$subject", "$message", "$headers");
echo("A confirmation email has been sent to: $email");
?>

diego715
02-09-2011, 05:35 AM
What version of php you are using ? the move uploaded files in some php versions are different the sintaxys may change

cocoonfx
02-09-2011, 07:59 AM
Hi I am using 5.2.13

diego715
02-09-2011, 03:16 PM
The same problem happened to me and i solved only adding the name in the second argument of move_uploaded_file

move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path.$_FILES['uploadedfile']['name']);

Hope this help