【发布时间】:2020-12-22 01:10:39
【问题描述】:
这是表格:
<form id="complaintForm" enctype="multipart/form-data" method="POST">
<input type="number" id="isComplaintForm" value="1" hidden>
<label for="fname">Full Name</label><br />
<input type="text" id="fname" name="fname"><br />
<label for="DOB">Date of Birth</label><br />
<input type="date" id="DOB" name="DOB"><br /><br />
<label for="identification">Valid ID card or passport No.</label><br />
<input type="number" id="identification" name="identification"><br /><br />
<label for="address">Full address of permanent place of residence</label><br />
<input type="text" id="address" name="address"><br /><br />
<label for="email">Email address and method of reply.</label><br />
<input type="email" id="email" name="email"><br /><br />
<label for="shortdesc">Short description of incident.</label><br/>
<input type="text" id="shortdesc" name="shortdesc"><br/><br/>
<label for="incident">Description of Incident.</label><br />
<textarea id="incident" name="incident" rows="6" cols="50"></textarea><br /><br />
<label for="uploaded_file">Select A File To Upload:</label><br />
<input type="file" id="uploadedFile" name="uploadedFile"><br /><br />
<input type="submit" value="Submit" id="complaintSubmit">
</form>
Ajax/Javascript:
submitHandler: function(form) {
// removes default submit events and establishes AJAX call
event.preventDefault();
var brand = faqApp.vars.brand;
// show loading bars
$("#callmeback_confirmation").css(cmbEnableCSS).html('<div class="sp sp-circle"></div>').show();
//var form = ($("#complaintForm")[0]);
var form = document.getElementById("complaintForm");
var formMessages = $('#callmeback_confirmation');
//Getting Files Collection
var file = $("#uploadedFile")[0].files;
var formData = new FormData();
formData.append("mail_id", $('#identification').val());
formData.append("mail_name", $('#fname').val());
formData.append("mail_address", $('#address').val());
formData.append("mail_dob", $('#DOB').val());
formData.append("cmb_email", $('#email').val());
formData.append("mail_subject", $('#shortdesc').val());
formData.append("mail_message", $('#incident').val());
formData.append("isComplaintForm", $('#isComplaintForm').val());
formData.append("attachment", $("input[type=file]")[0].files[0]);
for (var pair of formData.entries()) {
console.log(pair[0] + ', ' + pair[1]);
}
//var object = {};
//formData.forEach(function(value, key) {
//object[key] = value;
//});
//var fd = JSON.stringify(object);
//console.log(fd);
$.ajax({
type: 'POST',
timeout: 15000,
url: faqApp.vars.host + '/cmb.php',
global: false,
//dataType: 'json'
// brand and lang are defined within the code, I had no issue with them before I started working on including an attachment within the complaint form
data: {
brand: faqApp.vars.brand,
lang: faqApp.vars.language,
formData: formData
},
processData: false, // tell jQuery not to process the data
contentType: false,
cache: false
php:
$brands = array("abc = abc.com");
$form = $_POST['fd']; // form data stored as array
$brandLang = strtoupper($_POST['brand']."-".$_POST['lang']);
$lang = $_POST['lang'];
//playground
$formFD = $_POST['fd'];
echo "Vardumps ";
echo "</br> $_POST";
var_dump($_POST['fd']);
var_dump($_POST['formData']);
echo "</br> $_FILES";
var_dump($_FILES);
echo "</br>";
print_r($_POST);
if(isset($_POST)){
echo "Post is set";
var_dump($_POST);
var_dump($_FILES);
} else{
echo "Post is not set";
}
输出:
//console.log for loop in formData outputs the following
mail_id, 213123
mail_name, bob ross
mail_address, bob's house
mail_dob, 1111-11-11
cmb_email, bob@gmail.com
mail_subject, bob subject
mail_message, desc
isComplaintForm, 1
attachment, [object File]
//var fd = JSON.stringify(object) prints the following
{"mail_id":"213123","mail_name":"bob ross","mail_address":"bob's house","mail_dob":"1111-11-11","cmb_email":"bob@gmail.com","mail_subject":"bob subject","mail_message":"desc","isComplaintForm":"1","attachment":{}}
var_dumps 基本上什么都不输出,这些是 php 文件显示的注意事项:
Notice: Undefined index: fd
Notice: Undefined index: brand
Notice: Undefined index: lang
Notice: Undefined index: lang
Notice: Undefined index: fd
Vardumps
Array
Notice: Undefined index: fd
NULL
Notice: Undefined index: formData
NULL
Arrayarray(0) { }
Array ( ) Post is setarray(0) { } array(0) { }
这最终的范围是能够使用 $_FILES 作为文件发送带有附件的电子邮件,并使用 $_POST 作为文本数据。但首先我需要弄清楚为什么 $_POST 不包含 fd 或 formData。 PS:我不确定 var fd = JSON.stringify(object);对于这种情况是否必要。
任何帮助将不胜感激,因为我已经坚持了一段时间了。
另外,“请求有效负载”正在返回 [object Object]
【问题讨论】:
-
您分配给
FormData对象的字段名称与PHP 中的预期字段看起来不同 -
您不能将
formData嵌套在常规对象中。而如果data:是常规对象,则不能使用processData: false -
你需要把所有参数放在
FormData对象中,然后使用data: formData -
@Barmar 我正在使用 processData:false,因为我发现上传文件时需要这样做。在我之前没有上传文件的实现中,我没有 processData, contentType:false 并且没有使用 FormData 对象,而是在我的 javascript 中使用了 var formData = {mail_id: $('#identification').val()} 和它工作得很好
-
您必须使用
FormData对象来上传文件。所以你需要把 all 的参数放在那个对象中,而不是把它嵌套在一个普通的对象中。
标签: javascript php html ajax