我想在我的工作申请表中添加文件上传功能。现在一切正常,我收到一封邮件,但我想添加简历上传选项。我为此制作了输入类型的文件,但我不知道如何获取该文件并将其作为附件发送到我的邮件中。谢谢你。
这是我的AJAX代码:
var sucessMessagee = $("#sucessMessage");
$("#contact_form").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
console.log('Invalid form');
} else {
event.preventDefault();
submitForm();
}
});
function submitForm(){
var name = $("#name").val();
var surname = $('#surname').val();
var email = $("#email").val();
var year = $("#year").val();
var month = $("#month").val();
var day = $("#day").val();
var adress = $("#adress").val();
var phone = $("#phone").val();
$.ajax({
type: "POST",
url: "/php/jobs.php",
data: "name=" + name + "&surname=" + surname + "&email=" + email + "&year=" + year + "&month=" + month + "&day=" + day + "&adres=" + adress + "&phone=" + phone,
success : function(text){
if (text == "success"){
formSuccess();
} else {
console.log('Failed message');
}
}
});
};
function formSuccess(){
$("#contact_form")[0].reset();
$("#posao1").css("display", "none");
showMessage();
setTimeout(function() {sucessMessagee.css('display', 'none')},4000);
};
function showMessage() {
sucessMessagee.css('display', 'block');
};
这是我的php代码:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$result = $_POST['result'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$adress = $_POST['adress'];
$phone = $_POST['phone'];
/*if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
$file = "attachment/" . basename($_FILES['attachment']['name']);
move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
} else
$file = "";*/
$mail = new PHPMailer;
$mail->setFrom($email, 'Prijava za posao');
$mail->addAddress('luka9825@hotmail.com', 'Office');
$mail->Subject = 'Prijava za posao sa sajta proenterijer.rs';
$mail->Body = "Ime i prezime: $name $surname
\nDatum rođenja: $day.$month.$year
\nAdresa stanovanja: $adress
\nKontakt telefon: $phone
\nE-Mail adresa: $email";
if(!$mail->send()) {
} else {
echo "success";
}
?>
答案 0 :(得分:0)
您将需要对submitForm()
函数进行一些小的修改,如下所示:
function submitForm(){
var name = $("#name").val();
var surname = $('#surname').val();
var email = $("#email").val();
var year = $("#year").val();
var month = $("#month").val();
var day = $("#day").val();
var adress = $("#adress").val();
var phone = $("#phone").val();
var form_data = new FormData();
form_data.append('file', $('#ID_OF_YOUR_FILE_INPUT').prop('files')[0]);
form_data.append('name',name);
form_data.append('surname',surname);
form_data.append('email',email);
form_data.append('year',year);
form_data.append('month',month);
form_data.append('day',day);
form_data.append('adress',adress);
form_data.append('phone',phone);
$.ajax({
type: "POST",
url: "/php/jobs.php",
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
success : function(text){
if (text == "success"){
formSuccess();
} else {
console.log('Failed message');
}
}
});
};
您的PHP必须修改为类似的内容:
<?php
require 'phpmailer/PHPMailerAutoload.php';
$result = $_POST['result'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$adress = $_POST['adress'];
$phone = $_POST['phone'];
$mail_attachment = $_FILES['file']; // Observe, you don't have to upload the file to your server
$mail = new PHPMailer;
$mail->setFrom($email, 'Prijava za posao');
$mail->addAddress('luka9825@hotmail.com', 'Office');
$mail->addAttachment($mail_attachment);
$mail->Subject = 'Prijava za posao sa sajta proenterijer.rs';
$mail->Body = "Ime i prezime: $name $surname
\nDatum rođenja: $day.$month.$year
\nAdresa stanovanja: $adress
\nKontakt telefon: $phone
\nE-Mail adresa: $email";
if(!$mail->send()) {
} else {
echo "success";
}
?>
答案 1 :(得分:0)
super(Meta2, mcl).__new__(type, name, bases, attrs)
现在,您可以从php中的// Variable to store your files
var files;
// Add events
$('input[type=file]').on('change', prepareUpload);
// Grab the files and set them to our variable
function prepareUpload(event)
{
files = event.target.files;
}
$('form').on('submit', uploadFiles);
// Catch the form submit and upload the files
function uploadFiles(event)
{
event.stopPropagation(); // Stop stuff happening
event.preventDefault(); // Totally stop stuff happening
// START A LOADING SPINNER HERE
// Create a formdata object and add the files
var data = new FormData();
$.each(files, function(key, value)
{
data.append(key, value);
});
$.ajax({
url: 'submit.php?files',
type: 'POST',
data: data + "name=" + name + "&surname=" + surname + "&email=" + email + "&year=" + year + "&month=" + month + "&day=" + day + "&adres=" + adress + "&phone=" + phone,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR)
{
if(typeof data.error === 'undefined')
{
// Success so call function to process the form
submitForm(event, data);
}
else
{
// Handle errors here
console.log('ERRORS: ' + data.error);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
console.log('ERRORS: ' + textStatus);
// STOP LOADING SPINNER
}
});
}
获取文件,然后使用该文件通过电子邮件发送。添加