我遇到了如何在工作表单中添加附件的问题,我使用 Bootstrap,JQuery和PHP。我不知道如何添加附件,用户将附加他们的简历,然后将其发送到电子邮件。 我希望你能在不改变我的整个代码的情况下帮助我。它甚至可能吗?。
这是我的初始输入表格
<section class = "resume section-padding" id = "resume "><div class="form-group col-md-6">
<label for="fName">First Name</label>
<input type="text" class="form-control" id="fName" placeholder="First Name">
</div>
<div class="form-group col-md-6">
<label for="lName">Last Name</label>
<input type="text" class="form-control" id="lName" placeholder="Last Name">
</div>
<div class = "form-group col-md-12">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" placeholder="Address">
</div>
<div class = "form-group col-md-4">
<label for="doBirth">Date of Birth</label>
<input type="date" class="form-control" id="doBirth" placeholder="Date of Birth">
</div>
<div class = "form-group col-md-4">
<label for="city">City</label>
<input type="text" class="form-control" id="city" placeholder="City">
</div>
<div class = "form-group col-md-4">
<label for="zipCode">ZIP CODE</label>
<input type="number" class="form-control" id="zipCode" placeholder="ZIP">
</div>
<div class = "form-group col-md-6">
<label for="email">E-mail</label>
<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class = "form-group col-md-6">
<label for="phone">Phone</label>
<input type="number" class="form-control" id="phone" placeholder="Phone Number">
</div>
</div>
这是我的jquery,它将获得表单的值。
$('.resume #getResume').on('click', function(e){
e.preventDefault();
var fName = $('#fName').val();
var lName = $('#lName').val();
var address = $('#address').val();
var doBirth = $('#doBirth').val();
var city = $('#city').val();
var zipCode = $('#zipCode').val();
var email = $('#email').val();
var phone = $('#phone').val();
var form = new Array({ 'fName':fName, 'lName':lName, 'address':address, 'doBirth':doBirth, 'city':city, 'zipCode':zipCode,
'email':email, 'phone':phone });
$.ajax({
type: 'POST',
url: "/path/to/url.php",
data: ({'action': 'resume', 'form': form})
}).done(function(data) {
$('#resume .result').html(data);
$(".resume")[0].reset();
});
});
这是我的php,我的jQuery将传递值
$sendTo = "email@email.com";
$action = $_POST['action'];
$name = $_POST['form'][0]['name'];
$email = $_POST['form'][0]['email'];
$subject = $_POST['form'][0]['subject'];
$message = $_POST['form'][0]['message'];
if ($name == "" || $email == "" || $message == "" || $subject == "") {
echo "<p class=\"error\">There was problem while sending E-Mail. Please verify entered data and try again!</p>";
exit();
}
else
{
$header = 'From: ' . $name . '<' . $email . ">\r\n" .
'Reply-To: ' . $email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$sent = mail($sendTo, $subject, $message, $header);
if ($sent) {
echo "<p class=\"success\">Message sent succesfully.</p>";
} else {
echo "<p class=\"error\">There was problem while sending E-Mail.</p>";
}
}
?>
答案 0 :(得分:0)
如果您没有使用任何框架,并且在核心php中,上传文件非常容易。您可以查看所有代码并使用here以便更好地理解。这是完成任务的好资源。
在您当前的代码中,只需添加一个字段即可上传文件。喜欢这个
<input type="file" name="fileToUpload" id="fileToUpload">
。也不要忘记ON
php.ini
文件中的file_uploads。
要在电子邮件中添加
用于添加电子邮件只是捕获脚本文件中的文件并将其发送到这样的电子邮件。
$email->AddAttachment( $file_to_attach , 'NameOfFile.pdf' );