Access checkbox of a php table

1

as I can access the elements of type checkbox and fence me adding in a input the number of selected rows as you can see in the image.

My table:

<tbody>
                                <?php if(!empty($tarimas)): ?>
                                    <?php foreach($tarimas as $tarimas): ?>
                                    <tr role="row" class="odd">
                                        <td tabindex="0" class="sorting_1"><input type="hidden" value="<?php echo $tarimas->id;?>" name='inputseleccion[]'><?php echo $tarimas->id; ?></td>
                                        <td><span class="badge bg-green"><?php echo $tarimas->peso; ?></span> kgs</td>
                                        <td><?php echo $tarimas->cajas; ?> cajas</td>
                                        <td>
                                            <div class="checkbox">
                                            <label>
                                              <input type="checkbox" value="" name='inputselect[]' class="chkseleccion"> Añadir
                                            </label>
                                            </div>
                                      </td>
                                    </tr>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>

I guess it's with javascript or jquery but how can I access it and know if it has been changed to checked? to tell me how to access and bring the results of the checkered row would help me a lot.

    
asked by DoubleM 18.04.2018 в 01:54
source

2 answers

3

You can use Jquery:

Step 1: Include the library before closing the body content of the page:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>

Step 2: You must add the following structure to your table, since to take the value of the Pallet Weight and Pallet Box fields of your table, classes and separator elements must be added to the values of these rows. For this example I have added the class kilogram and the class cant-boxes to take the value of the fields.

Your table:

<tbody>
    <?php if(!empty($tarimas)): ?>
    <?php foreach($tarimas as $tarimas): ?>
    <tr role="row" class="odd">
        <td tabindex="0" class="sorting_1">
            <input type="hidden" value="<?php echo $tarimas->id;?>" name='inputseleccion[]'>
            <?php echo $tarimas->id; ?>
        </td>
        <td>
            <span class="badge bg-green kilogramo">
                <?php echo $tarimas->peso; ?>
            </span> kgs</td>
        <td>
            <span class="cant-cajas"><?php echo $tarimas->cajas; ?></span> cajas</td>
        <td>
            <div class="checkbox">
                <label>
                    <input type="checkbox" value="" name='inputselect[]' class="chkseleccion"> Añadir
                </label>
            </div>
        </td>
    </tr>
    <?php endforeach; ?>
    <?php endif; ?>
</tbody>

Step 3: Include the following script after jquery of step 1 :

<script type="text/javascript">
$(document).ready(function () {
    var verificarTotal = function () {
        var $seleccionados = $(".chkseleccion:checked"),
            $cajas = $("#tuInputTotalCaja"),
            $kilogramos = $("#tuInputTotalKilogramos"),
            totalCajas = 0,
            totalKilogramos = 0;

        $.each($seleccionados, function (indice, fila) {

            if ($(fila).is(':checked')) {
                totalCajas += parseInt($(fila).closest('tr').find('.cant_cajas').html());
                totalKilogramos += parseInt($(fila).closest('tr').find('.kilogramo').html());
            }
        });

        $cajas.val(totalCajas);
        $kilogramos.val(totalKilogramos);
    };

    $(".chkseleccion").on('click', function (e) {
        verificarTotal();
    });

    verificarTotal();
});

    
answered by 18.04.2018 / 02:39
source
0

With jQuery you would do it this way:

$(".chkseleccion:checked").length;

Using jQuery, you could also add a class to the row when you click on the checkboxes. That way it would be easy to identify the checked rows and bring all the information you need.

    
answered by 18.04.2018 в 01:58