Jquery收集表单数据并发送到php

时间:2017-09-15 05:31:53

标签: javascript php jquery

我有这段代码:

var data = [];
var workerEdit = {
    'id':'',
    'name': '',
    'photo': '',
    'bibliography': '',
    'history': '',
    'salary': ''
}
workerEdit.id = ""+id+"",
workerEdit.name = $('#workers_response input[name="name"][data-id="gear-'+id+'"]').val(),
workerEdit.photo = $('#workers_response input[name="pas-scan"][data-id="gear-'+id+'"]')[0].files[0],
workerEdit.bibliography = $('#workers_response textarea[name="bibliography"][data-id="gear-'+id+'"]').val(),
workerEdit.history = $('#workers_response textarea[name="history"][data-id="gear-'+id+'"]').val(),
workerEdit.salary = $('#workers_response input[name="salary"][data-id="gear-'+id+'"]').val()
data.push(workerEdit);

我如何将我的数据var转换为FormData并将其发送到php抛出ajax?

1 个答案:

答案 0 :(得分:0)

试试这个:

var data = {
  'id':'1',
  'name': '2',
  'photo': '3',
  'bibliography': '4',
  'history': '5',
  'salary': '6'
};
var formData= new FormData();
Object.keys(data).forEach(function(key) {
  formData.append(key, data[key]);
})
for(var part of formData.entries()){
  console.log(`key: ${part[0]}, value: ${part[1]}`);
}

Object.keys(@object)将返回一个数组,其中包含您传递的对象的键。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys