我认为对于有合适眼力的人来说这是一个非常快速的问题......尽管做了很多调查,但我自己一直在撞墙。
我有一个带有文件附件字段的表单,我想在不离开页面的情况下发送,即使用jquery / ajax来执行此操作。我知道它肯定是文件附件的混乱,因为当我删除所有引用它的功能完美,但我不知道该怎么做。
HTML基本上是一个普通的形式,所以我不会为此烦恼。这里是jquery / ajax的东西:
$(form).submit(function(){
$.post($(this).attr('action'), $(this).serialize(), function(response){
// success stuff
},'json');
return false;
});
我已经知道导致问题的数据序列化,因为文件无法序列化。有没有人对我该做什么有任何建议?如果可能的话,我想使用$ .post函数而不是$ .ajax函数,纯粹是为了它的简单性。我还没有找到一个使用$ .post功能的解决方案,保留在同一页面并允许文件附件。
万一它真的很容易做,如果有人能够提供适用于多个文件的解决方案(而不仅仅是一个),包括我需要对PHP文件做什么我和真的,非常感激,但我不想占用太多人的时间!这里是PHP以防万一有人能够帮助解决这个问题(标题位接近底部)。 FWIW目前php文件工作正常,即表单上的标准提交然后将其传递给php文件:
<?php
error_reporting(E_ALL);
//Only process POST requests
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace
$name = strip_tags(trim($_POST["name"])); // Remove tags from field
$name = str_replace(array("\r","\n"), array(" "," "),$name); // Replace carriage returns and newlines with spaces
$name = filter_var($name, FILTER_SANITIZE_STRING);
$email = $_POST["email"];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitize email address
$phonenumber = filter_var($_POST["phonenumber"],FILTER_SANITIZE_NUMBER_INT);
$fireservice = $_POST["fireservice"];
$customername = filter_var($_POST["customername"],FILTER_SANITIZE_STRING);
$customeraddress1 = $_POST["customeraddress1"];
$customeraddress2 = $_POST["customeraddress2"];
$customeraddress3 = $_POST["customeraddress3"];
$customeraddress4 = $_POST["customeraddress4"];
$customerphonenumber = filter_var($_POST["customerphonenumber"],FILTER_SANITIZE_NUMBER_INT);
$carername = $_POST["carername"];
$carerphonenumber = filter_var($_POST["carerphonenumber"],FILTER_SANITIZE_NUMBER_INT);
$hoodheight = $_POST["hoodheight"];
$additionalinfo = $_POST["additionalinfo"];
// Set the recipient email address
$recipient = "my.email@website.com";
// Set the email subject
$subject = "New Order from $name at $service";
$br = "<br>";
echo $name;
// Input the form information
$forminformation = "Contact Information:$br$br";
$forminformation .= "Name: $name$br";
$forminformation .= "Email: $email$br";
$forminformation .= "Phone number: $phonenumber$br";
$forminformation .= "Fire Service: $fireservice$br$br";
$forminformation .= "Customer information:$br$br";
$forminformation .= "Name: $customername$br";
$forminformation .= "Address line 1: $customeraddress1$br";
$forminformation .= "Address line 2: $customeraddress2$br";
$forminformation .= "County: $customeraddress3$br";
$forminformation .= "Postcode: $customeraddress4$br$br";
$forminformation .= "Carer Information:$br$br";
$forminformation .= "Name: $carername$br";
$forminformation .= "Phone number: $carerphonenumber$br$br";
$forminformation .= "Other Information:$br$br";
$forminformation .= "Height of hood(cm): $hoodheight$br";
$forminformation .= "Additional Info: $additionalinfo$br";
$uid = md5(uniqid(time()));
$filebasename = basename($file);
$eol = PHP_EOL;
// Basic headers
$header = "From: ".$name." - $fireservice Fire Service".$eol;
$header .= "Reply-To: ".$email.$eol;
$header .= "MIME-Version: 1.0".$eol;
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
// Message section
$body = "--".$uid.$eol;
$body .= "Content-Type: text/html; charset=ISO-8859-1".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $forminformation;
$body .= $eol.$eol;
$body .= "--".$uid.$eol;
// Send the email
if (mail($recipient, $subject, $body, $header)) {
// Set a 200 (OK) response code
header("HTTP/1.0 200 OK");
echo "Thank you. Your order has been received.";
unlink($file);
} else {
// Set a 500 (internal server error) response code
header("HTTP/1.0 500 Internal Server Error");
echo "Sorry, something went wrong, please check all form elements are correctly filled in and try again";
}
$confirmationheader = "From: Orders at Website".$eol;
$confirmationheader .= "Reply-To: orders@website.com".$eol;
$confirmationheader .= "MIME-Version: 1.0".$eol;
$confirmationheader .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";
$confirmationmessage = "Thank you for your order with Website. Someone will be in touch shortly";
mail($email, "Thank you for your order", $confirmationmessage, $confirmationheader);
} else {
// Not a POST request, set a 403 (forbidden) response code
header("HTTP/1.0 403 Server Code - forbidden");
echo "There was a problem with your submission, please try again.";
}
?>
谢谢!! 标记