var name = jQuery('[data-field="name"]').val();
var email = jQuery('[data-field="email"]').val();
var inumber = jQuery('[data-field="inumber"]').val();
var rnumber = jQuery('[data-field="rnumber"]').val();
var date = jQuery('[data-field="date"]').val();
var amount = jQuery('[data-field="amount"]').val();
var feedback = jQuery('[data-field="name"]').val();
var file_attach = new FormData(jQuery('input[name^="media"]'));
jQuery.each(jQuery('input[name^="media"]')[0].files, function(i, file) {
file_attach.append(i, file);
});
jQuery.ajax({
type: 'POST',
data: { func: "sendMail", name,email,inumber,rnumber,date,amount,feedback,file_attach},
url: 'billing-information-mailer.php',
cache: false,
contentType: false,
processData: false,
success: function(data){
console.log(data);
}
});
I'm trying to pass form info and attachment to a php file to send an email, initially I just went with $.post
however I need to set processData to false so I shifted to $.ajax
, currently my PHP file is sending an empty return. the only code in my php file is print_r($_POST);
EDIT:
<input type="file" name="media" data-field="billing" id="vdnupload" name="vdnupload" placeholder="Proof of Payment">
The syntax of the object you're providing to the data
property is incorrect as each item needs a key.
However a more pressing problem is that to upload a file you need to put all the data (file and input values) in to the FormData
object and send that alone. Try this:
var formData = new FormData();
$.each($('input[name^="media"]')[0].files, function(i, file) {
formData.append(i, file);
});
formData.append('name', $('[data-field="name"]').val());
formData.append('email', $('[data-field="email"]').val());
formData.append('inumber', $('[data-field="rnumber"]').val());
formData.append('rnumber', $('[data-field="rnumber"]').val());
formData.append('date', $('[data-field="date"]').val());
formData.append('amount', $('[data-field="amount"]').val());
formData.append('feedback', $('[data-field="name"]').val());
$.ajax({
type: 'POST',
data: formData,
url: 'billing-information-mailer.php',
cache: false,
contentType: false,
processData: false,
success: function(data){
console.log(data);
}
});
You could make this much, much simpler if you place a form
element around all the inputs you want to include in the request. That way you can just provide a reference of that form to the FormData
constructor and all values will be populated for you:
var formData = new FormData($('#myForm')[0]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.