Enter a value in the SQL table for each selected checkbox

0

I have a form with a checkbox group (same as below) and I would like that when I select the values, insert a row in the SQL table for each selected checkbox:

<form>
   <input type="hidden" id="id_pedido" value="123">
   <div>
     <input class="uk-checkbox" type="checkbox" value="16.15"> Adicional 1
   </div>
   <div>
     <input class="uk-checkbox" type="checkbox" value="23.75"> Adicional 2
   </div>
   <button id="concluir" name="concluir" class="uk-button uk-button-primary" type="submit">Concluir</button>
</form>

Then when selecting the 2 values, the MYSQL table (cv_contratos_adicional) should look like this:

id  |  id_pedido  |  valor   |  servico
1   |     123     |   16.15  |  Adicional 1
2   |     123     |   23.75  |  Adicional 2

This is what I have so far, the part of the checkbox and I need to know how to "break" the value and name of the service:

<body>
     <?php
    if(isset($_POST['concluir'])){
        $sql = "INSERT INTO cv_contratos_adicinoais (id_pedido, valor, servico)
        VALUES ('".$_POST["id_pedido"]."','".$_POST["valor"]."','".$_POST["servico"]."')";
    }

    ?>

    <form method="post"> 
       <input type="hidden" name="id_pedido" id="id_pedido" value="123">
       <div>
         <input class="uk-checkbox" type="checkbox" value="16.15"> Adicional 1
       </div>
       <div>
         <input class="uk-checkbox" type="checkbox" value="23.75"> Adicional 2
       </div>
       <button id="concluir" name="concluir" class="uk-button uk-button-primary" type="submit">Concluir</button>
    </form>

</body>
    
asked by anonymous 24.01.2018 / 11:11

1 answer

0

You're doing right now add the name attribute as an array in the checkbox:

<input name="adicional[]" class="uk-checkbox" type="checkbox" value="16.15"> Adicional 1
<input name="adicional[]" class="uk-checkbox" type="checkbox" value="23.75"> Adicional 2

And on the backend loop the array that will return to insert the checkboxes that have been selected:

if(isset($_POST['concluir'])) {
    $sql = "";

    for($i = 0; $i < count($_POST["adicional"]); $i++) {
        $sql .= "INSERT INTO cv_contratos_adicinoais (id_pedido, valor, servico)
            VALUES ('".$_POST["id_pedido"]."','".$_POST["adicional"][$i]."','".$_POST["servico"]."');";
    }

    echo $sql;
}

This is the basic syntax for working with multiple checkboxes, to pass the additional ones you have some options ...

  • Add one more invisible checkbox for each add-on and, by JavaScript, enable and disable when the visible checkbox is clicked

  • In PHP, check the value of the checkbox and assign the value of the additional

  • If you use AJAX, you can add another attribute to the checkbox that will be read and passed to PHP

Problems: In 1st and 2nd situation you will have a good amount of extra code (10% with% s or% with% with% more%), in the 1st and 3rd you can easily change the value in the front -end, in addition to the values, the name should be validated in the backend.

The first one I find a bit unnecessary gambiarra. If your code has only two checkboxes and will not be added any more, I advise the second option. If your code may come to have some checkbox I recommend using AJAX.

    
14.04.2018 / 05:46