简体   繁体   中英

Append data to formData object

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?

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'];

我认为你只检查$_FILES ,还检查发布数据print_r($_POST)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM