saber
saber

Reputation: 346

send data with javascript FormData()

My code has one input tag and send it to my php file by using jquery FormData() now i want add another input tag and send more data. jquery it's:

var files = document.getElementById('data-file').files;
var data = new FormData();

$.each(files, function (keys, values) {
    data.append(keys, values);
});
$.ajax({
    url: 'upload.php',
    type: 'POST',
    data: data,
    cache: false,
    processData: false,
    contentType: false,
});

and html code is:

<input style="display:none;" type="file" name="data-file" id="data-file" onchange="image_preview(this)" multiple="multiple" />

i want change html to this and send input data to php file by using jquery FormData():

<input style="display:none;" type="file" name="data-file" id="data-file" onchange="image_preview(this)" multiple="multiple" />
<input type="hidden" name="imgpath" id="imgpath" value="<?php echo $imgpath; ?>"  />
<input type="hidden" name="id" id="id" value="<?php echo $id; ?>"  />

Upvotes: 0

Views: 262

Answers (1)

code-jaff
code-jaff

Reputation: 9330

You can use FormData's append method to add additional key values to the FormData as you did within $.each

for eg.

$.each(files, function (keys, values) {
    data.append(keys, values);
});

data.append('imgpath', $('#imgpath').val());
data.append('id', $('#id').val());

Upvotes: 1

Related Questions