Send checkbox as fix in ajax and process it in PHP

0

I have a problem when sending the value of several checkboxes through ajax.

For example I have:

<input type='checkbox' name='ids[]' class="checkBoxGroup" value='1' />
<input type='checkbox' name='ids[]' class="checkBoxGroup" value='2' />
<input type='checkbox' name='ids[]' class="checkBoxGroup" value='3' />
<input type='checkbox' name='ids[]' class="checkBoxGroup" value='4' />
<input type='checkbox' name='ids[]' class="checkBoxGroup" value='5' />

and to send it by ajax I do the following:

$.ajax({
        type: "POST",
        dataType: 'json',
        data: { 'entrega': $("#p_entrega").val(),
                'recibe': $("#p_recibe").val(),
                'visto_bueno': $("#visto_bueno").val(),
                'ids[]': $('[name="ids[]"]').serialize()
              },
        url: "<?php echo site_url();?>/reportes/acta/agregarActa",
        success : function(data) {
          console.log(data[0].id_acta);
        }
    });

But when processing it in PHP it arrives in the following way:

Array ( [0] => ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3&ids%5B%5D=4&ids%5B%5D=5 )
  • Is there any way I can arrive as an arrangement?
  • Is there a way to transform it into an arrangement?
asked by Juan Pinzón 18.05.2016 в 18:46
source

3 answers

1

Well reading a bit more the jquery documentation I came to find the serializeArray() function that creates an array of objects, ready to be encoded as a JSON string, so in the ajax call I send a string JSON and decode it in PHP to get an arrangement.

The changes in the previous ajax call are:

$.ajax({
        type: "POST",
        dataType: 'json',

        data: { 'entrega': $("#p_entrega").val(),
                'recibe': $("#p_recibe").val(),
                'visto_bueno': $("#visto_bueno").val(),
                'ids': JSON.stringify($('[name="ids[]"]').serializeArray())
              },
        url: "<?php echo site_url();?>/reportes/acta/agregarActa",
        success : function(data) {
          //console.log(data[0].id_acta);

        }
    });

And to process it in PHP I did it in the following way:

print_r(json_decode($_POST["ids"],TRUE));

With what I got the following output:

Array
(
    [0] => Array
        (
            [name] => ids
            [value] => 139
        )

    [1] => Array
        (
            [name] => ids
            [value] => 135
        )

    [2] => Array
        (
            [name] => ids
            [value] => 145
        )

)
    
answered by 18.05.2016 / 22:18
source
1

jQuery by default converts the parameters of data to a string, uses processData and contentType according to the type of data:

.ajax Documentation ()
.ajax code () in Github

$.ajax({
    ...
    processData: false,
    // en caso de ser necesario
    // contentType: ...
    ...
});
    
answered by 18.05.2016 в 19:03
0

You can also do the following:

//Aquí buscamos todos los inputs que tengan una clase llamada; checkBoxGroup
var a = document.querySelectorAll("input.checkBoxGroup");
//Ahora vamos hacer uso del Prototype de JS para digamos recorrer todo lo que se ha generado desde la variable a y lo devolvemos a la variable ids_
var ids_ = Array.prototype.map.call(a,function(x){ return x.value; });
$.ajax({
        type: "POST",
        dataType: 'json',
        data: { 'entrega': $("#p_entrega").val(),
                'recibe': $("#p_recibe").val(),
                'visto_bueno': $("#visto_bueno").val(),
                'ids': ids_ //aqui aplicamos los ids_ en Array
              },
        url: "<?php echo site_url();?>/reportes/acta/agregarActa",
        success : function(data) {
          //console.log(data[0].id_acta);

        }
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 19.05.2016 в 20:46