Checkbox with dependencies

0

In case the quantity and unlimited checkbox.

By checking the checkbox, a line with other checkboxes is released with the desired amount of that ingredient:

 Ingrediente               Quantidade

 [ ] Alface id#1           [X] 1x [ ] 2x [ ] 3x
 [X] Bacon id#2            [ ] 1x [ ] 2x [X] 3x
 [ ] Queijo Cheddar id#3   [X] 1x [ ] 2x [ ] 3x
 [X] Mostarda Dijon id#4   [ ] 1x [X] 2x [ ] 3x

.

Array
(
    [ingrediente_id] => Array
        (
            [0] => 2
            [1] => 4
        )

    [quantidade_id] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 1
            [3] => 2
        )

)

How do I just save the quantity_id that was checked by the checkbox corresponding to ingredient_id?

the ingredients that have not been marked may not appear in the quantity_id array

    
asked by anonymous 15.09.2014 / 16:47

2 answers

0

To get the respective amount of the selected ingredient you can pass the ingredient id in the name of quantidade_id then just make a foreach to get the ingredient that will be the array key and the quantity that is the value:

Example

form

<html>
    <head>
        <script type="text/javascript">
            function habilitarQuantidade(item){

                var acao = true;
                if(item.checked === true) acao = false;

                for(var i = 1; i<=3; i++){
                    document.getElementById(item.value+'_'+i).disabled = acao;
                }

           }
        </script>
    </head>


<form action="" method="post">
    <?php
    $ingrediente = array(1 => 'Alface', 2 => 'Bacon', 3 => 'Queijo Cheddar', 4 => 'Mostarda Dijon');

    foreach($ingrediente as $id => $descricao){
        $checkbox = sprintf('
        <input type="checkbox" name="ingrediente[]" value="%d"  onclick="habilitarQuantidade(this);" /> %s
QTD: 1 <input type="checkbox" id="%d_1" name="quantidade_id[%d]" value="1" checked="checked" disabled="true"/>
     2 <input type="checkbox" id="%d_2" name="quantidade_id[%d]" value="2" disabled="true"/>
     3 <input type="checkbox" id="%d_3" name="quantidade_id[%d]" value="3" disabled="true"/>
<br>', $id, $descricao, $id, $id, $id, $id, $id, $id);

        echo $checkbox;
    }    
?>
<br>
<input type="submit">
</form>

php file

<?php

if(isset($_POST['ingrediente'])){
    foreach($_POST['quantidade_id'] as $id_ingrediente => $qtd){
        echo 'ingrediente id: '. $id_ingrediente .' qtd: '. $qtd  .'<br>';
    }
}

The array received by is something like:

Array
(
    [ingrediente] => Array
        (
            [0] => 1
            [1] => 4
        )

    [quantidade_id] => Array
        (
                [1] => 1
                [4] => 3
id do ingrediente^     ^ quantidade 
        )

)
    
15.09.2014 / 16:58
0

You can check with is(':checked') of jquery which quantity was selected and only set it in your array

for(){//percorrendo todos os ingredientes    
    if($("#input").is(':checked')){
        array[x][ingrediente] = $("ingredienteXX").val();
        array[x][ingrediente][quantidade]  = $("#input").val();
    }
    x++;
}

I do not know if this would be the most correct way to mount the array, but so in php you could simply get the specific amount for each ingredient according to the index.

    
15.09.2014 / 17:49