问题
I have an HTML form that have several different fields and drop down menus. The form has several different text fields, a couple drop down menus, and a file upload. Once the form is submitted it is sent to me via email. Below is the HTML and the PHP submit.
I can't seem to figure out how to get the file to upload and then be sent to me as an email attachment. I would appreciate any help. Thanks
The html looks like this `
<label class="label" for="name">Name:</label> <input id="name" class="name" type="text" name="distributorName" />
<label class="label" for="distributorId">Distributor ID:</label> <input id="name" class="name" type="text" name="distributorId" />
<label class="label" for="email">Distributor Email:</label> <input id="email" class="name" type="text" name="email" />
<h2>Business Information</h2>
<label class="label" for="businessName">Business Name:</label>
<input id="name" class="name" type="text" name="businessName" />
<label class="label" for="phone">Business Phone Number:</label>
<input id="phone" class="name" type="text" name="phone" />
<label class="label" for="street">Address:</label>
<input id="street" class="name" type="text" name="street" />
<label class="label" for="city">City:</label> <input id="city" class="name" type="text" name="city" />
<div id="state">
<label id="state" class="label" for="state">State:</label>
<select class="dropdown" name="state">
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="TX">TX</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
</select>
</div>
<div id="zip">
<label id="zip" class="label" for="zip">ZIP Code:</label>
<input id="zip" class="name" type="text" name="zip" />
</div>
<label class="nobg" for="uploaded_file">Company Logo:</label>
<input class="nobg" type="file" name="uploaded_file" />
</div>
<div id="right">
<div style="width:50%; float:left;">
<label class="label" for="validOthers">Valid with other Coupons:</label>
<select class="dropdown" name="validOthers">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<label class="label" for="oneCoupon">One Coupon Per Customer:</label>
<select class="dropdown" name="oneCoupon">
<option value="yes">Yes</option>
<option value="no">No</option> </select>
</div>
<div style="width:50%; float:left;">
<label class="label" for="outOfarea">Out of Area Coupon:</label>
<select class="dropdown" name="outOfarea">
<option value="yes">Yes</option>
<option value="no">No</option> </select>
<label class="label" for="businessCategory">Business Category (Optional):</label>
<input style="width:200px;" type="text" name="businessCategory" />
</div>
<h2>Offer 1</h2>
<label class="label" for="offer1">Your Offer:</label>
<input type="text"class="offers" name="offer1" />
<label class="label" for="offer1Days">Number of days the offer will last for:</label>
<input class="days" type="text" name="offer1Days" />
<label class="label" for="offer1Quantity">Number of Coupons:</label>
<input class="qty" type="text" name="offer1Quantity" />
<h2>Offer 2</h2>
<label class="label" for="offer2">Your Offer:</label>
<input type="text" class="offers" name="offer2" />
<label class="label" for="offer2Days">Number of days the offer will last for:</label>
<input class="days" type="text" name="offer2Days" />
<label class="label" for="offer2Quantity">Number of Coupons:</label>
<input class="qty" type="text" name="offer2Quantity" />
<h2>Offer 3</h2>
<label class="label" for="offer3">Your Offer:</label>
<input type="text" class="offers" name="offer3" />
<label class="label" for="offer3Days">Number of days the offer will last for:</label>
<input class="days" type="text" name="offer3Days" />
<label class="label" for="offer3Quantity">Number of Coupons:</label>
<input class="qty" type="text" name="offer3Quantity" />
<input id="order" class="submit" type="submit" value="Submit" />
`
Here's the PHP submit
if(isset($_POST['email'])) {
$email_to = "joel@joelshawdesigns.com";
$email_subject = "test form Submission";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['distributorName']) ||
!isset($_POST['distributorId']) ||
!isset($_POST['email']) ||
!isset($_POST['businessName']) ||
!isset($_POST['phone']) ||
!isset($_POST['street']) ||
!isset($_POST['city']) ||
!isset($_POST['state']) ||
!isset($_POST['zip']) ||
!isset($_POST['validOthers']) ||
!isset($_POST['oneCoupon']) ||
!isset($_POST['outOfarea']) ||
!isset($_POST['businessCategory']) ||
!isset($_POST['offer1']) ||
!isset($_POST['offer1Days']) ||
!isset($_POST['offer1Quantity']) ||
!isset($_POST['offer2']) ||
!isset($_POST['offer2Days']) ||
!isset($_POST['offer2Quantity']) ||
!isset($_POST['offer3']) ||
!isset($_POST['offer3Days']) ||
!isset($_POST['offer3Quantity']
)) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$distributorName = $_POST['distributorName']; // not required
$distributorId = $_POST['distributorId']; // not required
$email_from = $_POST['email']; // not required
$businessName = $_POST['businessName']; // not required
$phone = $_POST['phone']; // not required
$street = $_POST['street']; // not required
$city = $_POST['city']; // not required
$state = $_POST['state']; // not required
$zip = $_POST['zip']; // not required
$validOthers = $_POST['validOthers']; // not required
$oneCoupon = $_POST['oneCoupon']; // not required
$outOfarea = $_POST['outOfarea']; // not required
$businessCategory = $_POST['businessCategory']; // not required
$offer1 = $_POST['offer1']; // not required
$offer1Days = $_POST['offer1Days']; // not required
$offer1Quantity = $_POST['offer1Quantity']; // not required
$offer2 = $_POST['offer2']; // not required
$offer2Days = $_POST['offer2Days']; // not required
$offer2Quantity = $_POST['offer2Quantity']; // not required
$offer3 = $_POST['offer3']; // not required
$offer3Days = $_POST['offer3Days']; // not required
$offer3Quantity = $_POST['offer3Quantity']; // not required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'Hmm...The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$distributorName)) {
$error_message .= 'Hmm...The name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$businessName)) {
$error_message .= 'The Business Name you entered does not appear to be valid.<br />';
}
$string_exp = "/^[0-9.'-]+$/";
if(!preg_match($string_exp,$phone)) {
$error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Order From: $distributorName\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Distributor Name: ".clean_string($distributorName)."\n";
$email_message .= "Distributor ID: ".clean_string($distributorId)."\n";
$email_message .= "Distributor Email: ".clean_string($email_from)."\n";
$email_message .= "Business Name: ".clean_string($businessName)."\n";
$email_message .= "Business Telephone: ".clean_string($phone)."\n";
$email_message .= "Business Street: ".clean_string($street)."\n";
$email_message .= "Business City: ".clean_string($city)."\n";
$email_message .= "Business State: ".clean_string($state)."\n";
$email_message .= "Business Zip: ".clean_string($zip)."\n";
$email_message .= "Valid with other coupons: ".clean_string($validOthers)."\n";
$email_message .= "One coupon per customer: ".clean_string($oneCoupon)."\n";
$email_message .= "Out of Area Coupon: ".clean_string($outOfarea)."\n";
$email_message .= "Business Category: ".clean_string($businessCategory)."\n";
$email_message .= "Offer 1: ".clean_string($offer1)."\n";
$email_message .= "Offer 1 Duration: ".clean_string($offer1Days)."\n";
$email_message .= "Offer 1 Quantity: ".clean_string($offer1Quantity)."\n";
$email_message .= "Offer 2: ".clean_string($offer2)."\n";
$email_message .= "Offer 2 Duration: ".clean_string($offer2Days)."\n";
$email_message .= "Offer 2 Quantity: ".clean_string($offer2Quantity)."\n";
$email_message .= "Offer 3: ".clean_string($offer3)."\n";
$email_message .= "Offer 3 Duration: ".clean_string($offer3Days)."\n";
$email_message .= "Offer 3 Quantity: ".clean_string($offer3Quantity)."\n";
if ( move_uploaded_file ($_FILES['uploaded_file'] ['tmp_name'],
"uploads/{$_FILES['uploadFile'] ['name']}") )
{ print '<p> The file has been successfully uploaded </p>';
}
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
回答1:
Amm... I don't see "" tag in your code. You must have it when you want to pass data with this form, at the current you just have a nice UI of a form but it doesn't actually work.
<form action="your_php_file.php" method="post" enctype="multipart/form-data">
Please notice that the "enctype" attribute is needed for uploading files, if you just want to send simple inputs you don't need it.
If you just cut those lines off , there are 2 options. 1. Send a URL of the file in the email content. (Easiest) 2. Add the file as an attachment (Check out this code: http://www.texelate.co.uk/blog/send-email-attachment-with-php/)
BTW, instead of having this HUGE condition of checking if "isset", why not writing an array with the field's names and a loop which check each and one of them?
来源:https://stackoverflow.com/questions/8198507/html-form-submit-how-to-file-upload-and-email