Checkbox with dependencies

Asked

Viewed 135 times

1

In case the unlimited amount of checkbox.

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

 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 only save the quantity_id that was marked by the checkbox corresponding to ingrediente_id?

unmarked ingredients cannot appear in the quantity_id array

  • 1

    By php use the function count()

  • yes by php...

  • I’ll try that function

2 answers

1

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

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

I don’t know if this would be the right way to assemble the array, but in php you could simply take the specific amount for each ingredient according to the index.

  • @peritto, it wouldn’t be like php

  • Thank you for your attention :D

0


To take the respective amount of the selected ingredient you can pass the ingredient id in the name of quantidade_id then just do a foreach to get the ingredient that will be the key to the array and the amount 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 
        )

)
  • quantity_id would have to save the value in the array only if the ingredient was marked... in case ta saving 4 values and not the 2 only that was marked

  • in this case ta returning what is the depth of this array

  • Array ( [ingrediente_id] => Array ( [0] => 2 [1] => 4 ) [quantidade_id] => Array ( [0] => 2 [1] => 3 ) ) in which case Bacon would be marked with 2 and Dijon Mustard would be marked with 3

  • ready altered

  • Remember that you need to use javascript to enable/disable quantity checkboxes.

  • more ai would be the same thing to take the value of ingrediente_id and put ingrediente_id -1 would take the value of the corresponding quantity_id field... but the id quantity array would become gigantic.. are more than 50 items

  • Array ( [ingrediente_id] => Array ( [0] => 2 [1] => 4 ) [quantidade_id] => Array ( [0] => 3 [1] => 2 ) ;)

  • @Willian Only marked items are sent to php.

  • ta ta agora que vi

  • So ingredient id: 1 Qtd: 1 ingredient id: 2 Qtd: 1 ingredient id: 3 Qtd: 1 ingredient id: 4 Qtd: 1 same thing leaving all... the ingredients in the array... pq the checkbox [1] 1x will always be marked because of the customer... sometimes do not mark not see right what is doing.. there you have seen neh...

  • the quantity will always be marked [X] 1x plus who will decide whether that quantity enters the quantity_id array and the ingredient, if it is not marked the quantity does not enter the quantity_id array so that only the ingredients are marked with their proper quantities

  • Thanks @lost worked :D

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.