1

I have a problem. formData.append("id_element",1) doesn`t add the field into formData. I have found a lot of solves but in my case it doesn`t work right.

PHP code here:

var_dump($_FILES);

$("#hw-upload_image-form").submit(function(e) {
        e.preventDefault();

        var formData = new FormData(this);
        formData.append("id_element",1); // doesn`t work here
        $.ajax({
            type:"POST",
            processData: false,
            contentType: false,
            cache: false,
            url:$(this).prop('action'),
            data:formData,
            success:function (data) {
                console.log(data); // show returned data from php
            }
        });

});
<form action="action.php"  method="post" enctype="multipart/form-data" id="hw-upload_image-form">
    <input type="file" name="hwImage"> <!-- hw = homework (just for you :) -->
</form>

Result (from the console)

array(1) {
  ["hwImage"]=>
  array(5) {
    ["name"]=>
    string(70) "73b38ef5d1f5849ea800c18990acde94_ce_1920x1200x0x0_cropped_800x427.jpeg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(36) "D:\OSPanel\userdata\temp\php9F48.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(68411)
  }
}
Nekear
  • 11
  • 2
  • Possible duplicate of [Uploading both data and files in one form using Ajax?](https://stackoverflow.com/questions/10899384/uploading-both-data-and-files-in-one-form-using-ajax) – ttrasn Jan 03 '19 at 21:10
  • You removed (stealth edit) a `console.log()` line that would actually be quite helpful in debugging. What was the result/output of that line? – Patrick Q Jan 03 '19 at 21:10
  • I removed it, because the output was: https://ibb.co/KN3bMyC (random site for share images) – Nekear Jan 04 '19 at 13:05

1 Answers1

0

I have already undestood my mistake.

$_FILES will show me only file data, but if I want to see id_element I have to use $_POST.

The result is:

array(1) {
  ["id_element"]=>
  string(1) "1"
}
Nekear
  • 11
  • 2