0

This is my ajax code

var formData = new FormData();
$a = array = ['1', '7'];
formData.append('kpmConfigDt1', $a);

$.ajax({
    url: '<?= base_url() ?>hr_pms/kpmConfigDtl_list1',
    data: formData,
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data) {
      .........
    }
});

this my code, I want to use $a as the value of my formData.append, but it doesn't work, can you help me? Thanks.

TarangP
  • 2,711
  • 5
  • 20
  • 41

2 Answers2

0

use a for loop to append

$length = count($a);
for ($i = 0; $i < $length; $i++) {
  formData.append('kpmConfigDt1', $a[$i]);
}
  • 1
    Thank you for this, but still I doesn't work, I have this controller that accept the value from formData public function kpmConfigDtl_list1(){ $kpmConfigDt = $this->input->post('kpmConfigDt1'); $kpmConfigDt2 = $this->pms_model->get_kpmConfigDt2($kpmConfigDt); echo json_encode($kpmConfigDt2); } – Jahlel_Ann Mar 14 '18 at 04:21
0

you need to serialize the array:

formData.append('name',array.toString());

Then you can recreate it on the php side

explode(',',$array);

I use the following query in a function designed to get all the form data:

var formData = new FormData();
$('#formId').find("input, textarea, select").each(function(){

    ...

    if(Array.isArray($(this).val())){
       formData.append(
          $(this).attr('name'),
          $(this).val().toString()
       ); 
    }

    ...

});
M31
  • 1,348
  • 12
  • 16