javascript - Append data to formData object - Stack Overflow

I seem not to get additionally data added to "formData".First i add my input file:var form =

I seem not to get additionally data added to "formData".

First i add my input file:

var form = $('#uploadForm')[0];
var formData = new FormData(form);

var input = $("#uploadPhoto")[0];
//Add input file data to formData
formData.append(input.name, input.files[0]);

This works just fine.
And my PHP var_dump after "ajax call" results:

array(1) {
  ["uploadPhoto"]=>
  array(5) {
    ["name"]=>
    string(5) "1.xls"
    ["type"]=>
    string(24) "application/vnd.ms-excel"
    ["tmp_name"]=>
    string(40) "..../tmp/phpmyn3E1"
    ["error"]=>
    int(0)
    ["size"]=>
    int(42799)
  }
}

Now i'd like to add some extra data for passing on to the php script:

formData.append('usr', selectedUsr);
formData.append(input.name, selectedUsr);
formData.append('usr', 'usr: '+ selectedUsr);

When i now check my PHP var_dump, there is no "usr" data in array.
Why?

I seem not to get additionally data added to "formData".

First i add my input file:

var form = $('#uploadForm')[0];
var formData = new FormData(form);

var input = $("#uploadPhoto")[0];
//Add input file data to formData
formData.append(input.name, input.files[0]);

This works just fine.
And my PHP var_dump after "ajax call" results:

array(1) {
  ["uploadPhoto"]=>
  array(5) {
    ["name"]=>
    string(5) "1.xls"
    ["type"]=>
    string(24) "application/vnd.ms-excel"
    ["tmp_name"]=>
    string(40) "..../tmp/phpmyn3E1"
    ["error"]=>
    int(0)
    ["size"]=>
    int(42799)
  }
}

Now i'd like to add some extra data for passing on to the php script:

formData.append('usr', selectedUsr);
formData.append(input.name, selectedUsr);
formData.append('usr', 'usr: '+ selectedUsr);

When i now check my PHP var_dump, there is no "usr" data in array.
Why?

Share Improve this question asked Jun 28, 2016 at 10:23 Björn CBjörn C 4,00811 gold badges52 silver badges87 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

When u append:

formData.append('usr', selectedUsr);

You cannot access the object with $_FILES['uploadPhoto']

Instead i access with $_POST

So my solution was:

//JS
formData.append('usr', selectedUsr);
//PHP
$usr = $_POST['usr'];

I think you are checking only $_FILES , also check post data print_r($_POST)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743702080a4492742.html

相关推荐

  • javascript - Append data to formData object - Stack Overflow

    I seem not to get additionally data added to "formData".First i add my input file:var form =

    16小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信