I have a form to upload a lot of information and a file multiupload uploader like this :
<div class="col-md-4">
<div class="form-group">
<label class="control-label col-md-3">Location</label>
<div class="col-md-9">
<?php
$location = array(
"type" => "text",
"name" => "location",
"id" => "location",
"class" => "form-control"
);
echo form_input($location);
?>
<span class="help-block"></span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label col-md-2">Warehouse</label>
<div class="col-md-10">
<?php
foreach ($tipe as $v):
echo "<label class='checkbox-inline'><input type='checkbox' name='tipe[]' value='$v->ID_CHECK_LIST'>$v->NAMA_CHECK_LIST</label>";
endforeach;
?>
<p class="help-block"></p>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label col-md-2">Image If Damage</label>
<div class="col-md-4">
<input type="file" multiple="" name="images[]">
<p class="help-block"></p>
</div>
</div>
Now, I need to send them using ajax. I have try $(form).serialized(), but the $_FILES is empty, So I use FormData class. But FormData just handle the file, not the another input. How can I set the data in aja parameter to handle file and another input.
This is the ajax jquery
$('#form').submit(function () {
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled', true); //set button disable
var url;
var formData = new FormData(this);
if (save_method === 'add') {
url = "<?php echo site_url('members/it/Request/ajax_add') ?>";
} else {
url = "<?php echo site_url('members/megumi/cek_list_wire_rod/ajax_update') ?>";
}
// ajax adding data to database
$.ajax({
url: url,
type: "POST",
data: formData,
processData: false,
contentType: false,
$('#form').serialize(),
dataType: "JSON",
success: function (data)
{
if (data.status) //if success close modal and reload ajax table
{
$('#modal_form').modal('hide');
reload_table();
} else
{
for (var i = 0; i < data.inputerror.length; i++)
{
$('[name="' + data.inputerror[i] + '"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="' + data.inputerror[i] + '"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('Save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error adding / update data');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
}
});
return false;
});
Any help it so appreciated.