0

I want to send array from checkbox via Jquery AJAX, but the response is not right.

here the html :

<input type="checkbox" id="krs_id_kelas" name="krs_id_kelas[]" value="0ec81bdf-1fc6-447d-ab65-bc67a857d99c">
<input type="checkbox" id="krs_id_kelas" name="krs_id_kelas[]" value="173867c3-5721-4aa2-9344-f5ad9fd05537">

Script

$(document).ready(function () {   
$('#form_krs_kolektif').submit(function (event) {

     var formData = {
            'krs_id_prodi': $('#krs_id_prodi').val(), //this part is fine
            'periode': $('#periode_krs option:selected').val(), //this part is fine
            'krs_id_regis_mhs': $('#krs_id_regis_mhs').val(), //this part is fine
            'id_kelas[]': $('#krs_id_kelas:checked').serialize() // only this part has a problem
        };
     $.ajax({
            type: 'POST',
            url: '<?=base_url()?>akademik/proses_krs_kolektif/',
            data: formData,
            dataType: 'json',
            encode: true
        })

    event.preventDefault();
    });

}); 

When I print_r the POST result from php part, the response from console is like this

Array
(
    [0] => krs_id_kelas%5B%5D=0ec81bdf-1fc6-447d-ab65-bc67a857d99c&krs_id_kelas%5B%5D=173867c3-5721-4aa2-9344-f5ad9fd05537
)

What I want is array like this, how can I fix it ?

Array
(
    [0] => 0ec81bdf-1fc6-447d-ab65-bc67a857d99c
    [1] => 173867c3-5721-4aa2-9344-f5ad9fd05537
)
4
  • 1
    first of all dont keep same id for both, change and check Commented Jul 18, 2017 at 4:36
  • I have a lot of checkbox and has a random data. I can't change all of them one by one. The only way is to send as array if the checkbox is checked Commented Jul 18, 2017 at 4:38
  • 1
    then give a dynamic id, as same id shouldnt exist in the same page Commented Jul 18, 2017 at 4:39
  • I just found the answer , I just need to parse_str the post result Commented Jul 18, 2017 at 4:51

2 Answers 2

3

HTML should be, instead of ID you must use class:

 <input type="checkbox" class="krs_id_kelas" name="krs_id_kelas[]" value="0ec81bdf-1fc6-447d-ab65-bc67a857d99c">
 <input type="checkbox" class="krs_id_kelas" name="krs_id_kelas[]" value="173867c3-5721-4aa2-9344-f5ad9fd05537">

try this script:

$(document).ready(function () {   
 $('#form_krs_kolektif').submit(function (event) {
 var chekedValue = [];
 $('.krs_id_kelas:checked').each(function(){
   chekedValue .push($(this).val());
 })
 var formData = {
        'krs_id_prodi': $('#krs_id_prodi').val(), //this part is fine
        'periode': $('#periode_krs option:selected').val(), //this part is fine
        'krs_id_regis_mhs': $('#krs_id_regis_mhs').val(), //this part is fine
        'id_kelas': chekedValue // only this part has a problem
    };
 $.ajax({
        type: 'POST',
        url: '<?=base_url()?>akademik/proses_krs_kolektif/',
        data: formData,
        dataType: 'json',
        encode: true
    })

event.preventDefault();
});

}); 

and print $_POST you will get the desired result.

Sign up to request clarification or add additional context in comments.

Comments

0

try this One
change from

var formData = {
    'krs_id_prodi': $('#krs_id_prodi').val(), //this part is fine
    'periode': $('#periode_krs option:selected').val(), //this part is fine
    'krs_id_regis_mhs': $('#krs_id_regis_mhs').val(), //this part is fine
    'id_kelas[]': $('#krs_id_kelas:checked').serialize() // only this part has a problem
};

to

var formData = $('#form_krs_kolektif').serialize();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.