Insert a value into the SQL table for each selected checkbox

Asked

Viewed 738 times

0

I have a form with a checkbox group (equal below) and would like when selecting 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_additional) 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 checkbox part 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>
  • and what have you tried to do? by the way, which server language is using?

  • to using php, I tried to use the INSERT INTO cv_contratos_additional, then it inserts, the problem is that from this comes all checkbox as a single value and not in separate lines

  • you need to put your complete code, the php you are using to insert and to assemble the page otherwise it will be difficult to help

  • edited, and put there what I have so far

  • put name in checkbox and recover with post, and alias where is the mysqli_query($sql) in your code?

1 answer

0

You’re doing it 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 in the back-end loop the array that will return to insert the checkboxs that were 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 to work with multiple checkboxs, to pass the additional you have some options...

  • Add another invisible checkbox for each additional and, by Javascript, enable and disable when the visible checkbox is clicked

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

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

Problems: in the 1st and 2nd situation you will have a good amount of extra code (10 inputs or if else/switch case a plus), in 1st and 3rd the values can be easily changed in front-end, in addition to the values, the name must be validated in the back-end.

The first one, I think, is a little out of the question. If your code has only two checkbox and will not be added any more, I advise the second option. If your code may have some checkbox I recommend using AJAX.

Browser other questions tagged

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