0

I want to pass imageArray[] along with a variable trackNo. The imageArray[] I can pass it fine with no problem. I'm just uncertain on how to append/include other variables to the FormData()

Online I only see examples of people passing only a file array to a php file and never with other information.

$('#ad_post_btn').click(function () {
    ad_errmsg = "";

    imageList = myImageList(); //my array of images imageList[]

    console.log(imageList); //display files in imageList[]

    var trackNo = Math.round(Math.random() * (100000 - 1) + 1); // random number

    console.log(trackNo); // shows the random generated number

    var data = new FormData(); 
    var dataString = 'trackno=' + trackNo; //the variable a want to pass along with the array

    for (var i = 0; i < imageList.length; i++) {
        data.append('images[]', imageList[i]); //where I store my image files, work fine
    }

    $.ajax({
        url: 'uploadimage.php',
        type: 'post',
        data: data,
        contentType: false,
        processData: false,
        success: function (data) {
            console.log(data);

        }
    });

I've tried

data: data,dataString ,

Thank you for your time

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

-1

Solution:

data.append("key", value);

Source:

https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects

Mark
  • 1,852
  • 3
  • 18
  • 31
-2

data.append('trackno', trackNo);

You can put this right before or after your for loop.

.append('key', value) will allow you to keep adding more fields.

https://developer.mozilla.org/en-US/docs/Web/API/FormData/append

Carlos Reyes
  • 136
  • 1
  • 10