Hi all,
Would very much appreciate it if you all could help me with a form I'm trying to build for a project. I'll post the code of the form and the php processor below.
Basically, I need the form to force users to fill out required fields, validate e-mail addresses AND allow users to send files (via the upload file button) which is all then sent to an email address.
Any help would be much appreciated.
Here's the code:
The form:
<form id="Enquiries" name="Enquiries" method="post" action="enquiryprocess3.php">
<tr>
<td colspan="2" align="center"><strong>*</strong> Indicates a required field.<br />
<?=$errorString?><br />
<? echo $email_error?></td>
</tr>
<tr>
<td><label for="Name">* Name:</label></td>
<td colspan="2"><input name="Name" type="text" id="Name" size="25" maxlength="80" value="<?=Name?>"/></td>
</tr>
<tr>
<td><label for="Company Name">Company Name:</label></td>
<td colspan="2"><input name="Company Name" type="text" id="CompanyName" size="25" maxlength="80" value="<?=CompanyName?>"/></td>
</tr>
<tr>
<td><label for="Telephone">* Telephone:</label></td>
<td colspan="2"><input name="Telephone" type="text" onkeyup="javascript:this.value=this.value.replace(/[^0-9]/g, '');" id="Telephone" size="25" maxlength="11" value="<?=Telephone?>"/></td>
</tr>
<tr>
<td height="26"><label for="Email">* Email:</label></td>
<td colspan="2"><input name="Email" type="text" id="Email" size="25" maxlength="80" value="<?=Email?>"/></td>
</tr>
<tr>
<td valign="top"><label for="Postal Address">Postal Address:</label></td>
<td colspan="2"><textarea name="Postal Address" id="PostalAddress" cols="30" rows="4" value="<?=PostalAddress?>"></textarea></td>
</tr>
<tr>
<td valign="top"><label for="Message">* Message:</label></td>
<td colspan="2"><textarea name="Message" id="Message" cols="30" rows="4" value="<?=Message?>"></textarea></td>
</tr>
<tr>
<td><label for="Upload File">Upload File:</label></td>
<td colspan="2"><input name="Upload File" id="UploadFile" type="file" value="<?=UploadFile?>"/></td>
</tr>
<tr>
<td></td>
<td align="right"><input name="Submit" id="Submit" type="submit" value="Send" /></td>
<td align="left"><input name="Reset" id="Reset" type="reset" value="Reset this form" /></td>
</tr>
</form>
The current process model:
<?php
/*
* Specify the field names that are in the form. This is meant
* for security so that someone can't send whatever they want
* to the form.
*/
$allowedFields = array(
'Name',
'CompanyName',
'Telephone',
'Email',
'Telephone',
'PostalAddress'
'Message',
'UploadFile',
);
// Specify the field names that you want to require...
$requiredFields = array(
'Name',
'Telephone',
'Email',
'Message',
);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $requiredFields['Email']))
{
$email_error("E-mail address not valid");
}
// Loop through the $_POST array, which comes from the form...
$errors = array();
foreach($_POST AS $key => $value)
{
// first need to make sure this is an allowed field
if(in_array($key, $allowedFields))
{
$$key = $value;
// is this a required field?
if(in_array($key, $requiredFields) && $value == '')
{
$errors[] = "The field $key is required.";
}
}
}
// were there any errors?
if(count($errors) > 0)
{
$errorString = '<p>There was an error processing the form.</p>';
$errorString .= '<ul>';
foreach($errors as $error)
{
$errorString .= "<li>$error</li>";
}
$errorString .= '</ul>';
// display the previous form
include 'contact.php';
}
else
{
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
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/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$body . "\n\n";
$body = "Dear Barry,
The following enquiry has been submitted from the website:
Name: $Name
Company: $CompanyName
Telephone: $Telephone
E-mail: $Email
Postal Address: $Address
Message: $Message
End of message
";
// 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
$to = "gwn@rich.com";
$subject = "Enquiry from Blissett Design, Print and Media Website";
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
$theResults = <<<EOD
<html>
<head>
<title>Blissett Design, Print and Media | Make An Enquiry</title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
left:300px;
top:50px;
width:750px;
height:450px;
z-index:1;
background-image: url("http://www.blissettdigital.co.uk/images/contact_bg.jpg");
}
#apDiv2 {
position:absolute;
left:332px;
top:50px;
width:649px;
height:378px;
z-index:2;
}
p {
font-family: Arial;
font-size: 14px;
color: #FFFFFF;
}
h1 {
font-family: Georgia;
font-size: 47px;
color: #D67445;
text-align: right;
}
#apDiv3 {
position:absolute;
left:-14px;
top:220px;
width:194px;
height:105px;
z-index:3;
}
-->
</style>
</head>
<body bgcolor="#000000">
<div id="apDiv1">
<div id="apDiv2">
<h1>Thank You</h1><hr />
<p> </p>
<p>Your message has been received. We will get back to you shortly.</p>
<p>Yours Sincerely,<br />
Blissett Design, Print and Media</p>
<div id="apDiv3"><img src="http://www.blissettditigal.co.uk/images/logo.png" /></div>
</div>
</div>
</body>
</html>
EOD;
echo "$theResults";
} else {
$theError = <<<EOD
<html>
<head>
<title>Blissett Design, Print and Media | Make An Enquiry</title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
left:300px;
top:50px;
width:750px;
height:450px;
z-index:1;
background-image: url("http://www.blissettdigital.co.uk/images/contact_bg.jpg");
}
#apDiv2 {
position:absolute;
left:332px;
top:100px;
width:649px;
height:378px;
z-index:2;
}
p {
font-family: Arial;
font-size: 14px;
color: #FFFFFF;
}
h1 {
font-family: Georgia;
font-size: 47px;
color: #D67445;
text-align: right;
}
#apDiv3 {
position:absolute;
left:-14px;
top:220px;
width:194px;
height:105px;
z-index:3;
}
-->
</style>
</head>
<body bgcolor="#000000">
<div id="apDiv1"></div>
<div id="apDiv2">
<h1>Sorry</h1><hr />
<p> </p>
<b>Your form could not be processed.</b><br />
Please re-submit the form.
<p><a href="enquiries.php">Click here to go back to the form</a></p>
</div>
</body>
</html>
EOD;
echo "$theError";
}
I keep getting a Parse error: syntax error, unexpected $end in /home/apparen3/public_html/enquiryprocess2.php on line 147 error, so I'd really appreciate the help!