Get ajax checkbox values

0

the following happens to me I am driving a system which has 2 checkboxes

<div class="form-group">
        <input id="admin" type="checkbox" class="material_checkbox" name="admin">
        <label for="admin">Administrador</label>
    </div>
    <div class="form-group">
        <input id="active" type="checkbox" class="material_checkbox" name="active">
        <label for="active">Activo</label>
    </div>

and subsequently I need to receive them in ajax as values 1 or 0. It should be noted that I'm working with jquery

    
asked by Josbert Hernandez 10.02.2017 в 02:30
source

1 answer

2

Good evening, if I understand correctly, do you need the value of both checkboxes in an arrangement to send by ajax, right?

If so, I think this code will serve you:

        var values = $('input:checkbox').map(function() {
        return this.value; // obtienes el valor de todos los checkboxes

        }).get();
       // Y aqui mandas los valores al archivo php
        $.ajax({
            type: "POST",
            url: 'accion.php',
            data: { valores : values },
             //valores = [1,0]
        });

and in your php it would be something like this:

        <?php
            $valores = $_POST["valores"];
             foreach $valor as $valores
             {
              // aqui va tu codigo
             }
          ?>

Greetings, any questions I am at your disposal

    
answered by 10.02.2017 / 02:54
source