How to store multiple checkbox

Asked

Viewed 413 times

1

I am creating a system and how could I store multiple checkbox and inputs in mysql ? I know I can do it this way but only if it goes to a column , in my case are four as could do

if(!Empty($_POST['genero']) && Count($_POST['genero']) ){ $items = implode(',', $_POST['gender']);

that’s my code

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $numero_mesa = $_POST['numero_mesa'];
    $pedido_refeicao = $_POST['pedido_refeicao'];
    $num_refeicao = $_POST['num_refeicao'];
    $pedido_bebida = $_POST['pedido_bebida'];
    $num_bebida = $_POST['num_bebida'];
        $sql="INSERT INTO mpedido(numero_mesa,pedido_refeicao,num_refeicao,pedido_bebida,num_bebida) VALUES('$numero_mesa','$pedido_refeicao','$num_refeicao','$pedido_bebida','$num_bebida')";
        $resultado_pedido = mysqli_query($link,$sql);

}
?>

inserir a descrição da imagem aqui

<fieldset>
<div class="pedidos">
<form method="post" action="secao.php">
<table >
    <tr>
        <td colspan="4" width="100">
         <span>Mesa</span>
         <input type="text" id="numero_mesa" name="numero_mesa">
         <label>
                    <span>Comanda:</span>

                </label>
                <br>
                <br>
        </td>

    </tr>
    <tr>
    <td width="280" bgcolor="#00BFFF">Refeição</td>
    <td width="70" bgcolor="#00BFFF">Quantidade</td>
    <td width="150" bgcolor="#1E90FF">Bebida</td>
    <td width="70" bgcolor="#1E90FF">Quantidade</td>
    </tr>

    <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Costela de Tambaqui sem Espinha">Costela de Tambaqui sem Espinha</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Fanta Laranja 1l">Fanta Laranja 1l</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Lombo de Tambaqui Frito sem Espinha">Lombo de Tambaqui Frito sem Espinha</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Fanta Laranja 2l">Fanta Laranja 2l</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Caldeirada de Tambaqui sem Espinha">Caldeirada de Tambaqui sem Espinha</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola 1l">Cola Cola 1l</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Caldeirada de Tucunaré">Caldeirada de Tucunaré</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola 2l">Cola Cola 2l</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Peixe no Tucupi com Camarão">Peixe no Tucupi com Camarão</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Bare 2l">Bare 2l</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Escabeche de Pirarucu">Escabeche de Pirarucu</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Fanta Uva">Fanta Uva</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Escabeche de Tambaqui">Escabeche de Tambaqui</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Fanta Laranja">Fanta Laranja</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Escabeche de Tucunaré">Escabeche de Tucunaré</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Sprit">Sprit</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Tucunaré Frito">Tucunaré Frito</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola">Cola Cola</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Sardinha Frita">Sardinha Frita</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Cola Cola zero">Cola Cola zero</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Jaraqui Frito">Jaraqui Frito</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Guaraná Antarctica">Guaraná Antarctica</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Pacu Frito">Pacu Frito</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Guaraná Baré">Guaraná Baré</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Filé de Pirarucu Frito">Filé de Pirarucu Frito</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Suco Manga">Suco Manga</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Filé de Pirarucu a Milanesa">Filé de Pirarucu a Milanesa</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Suco Pessego">Suco Pessego</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
        <tr>
        <td width="280"><input type="checkbox" name="pedido_refeicao[]" value="Guisado de Pirarucu">Guisado de Pirarucu</td>
        <td width="70"><input type="text" name="num_refeicao[]" size="7"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Suco Goiaba">Suco Goiaba</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Suco Uva">Suco Uva</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Suco Maracujá">Suco Maracujá</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Suco Laranja">Suco Laranja</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Suco Caju">Suco Caju</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Agua Mineral">Agua Mineral</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Agua com Gas">Agua com Gas</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Cerveja em Lata">Cerveja em Lata</td>
        <td width="70"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td width="280"></td>
        <td width="70"></td>
        <td width="150"><input type="checkbox" name="pedido_bebida[]" value="Limonada Natural">Limonada Natural</td>
        <td width="5"><input type="text" name="num_bebida[]" size="7"></td>
    </tr>
    <tr>
        <td colspan="4" width="100">
        <br>
         <button class="btn" type="submit">Fazer Pedido</button>  
        </td>
    </tr>

</table>


</div>

</fieldset>
   </form>
  • Each checkbox must possess a name different, you can choose a code to facilitate, at the time of saving I suggest you save in a field only as a array json for example, the quantity may work the same way, so at the time of pulling the order can bring only the listed items and the amounts referred.

  • Publish the HTML snippet from input checkbox

  • because it is every checkbox has different name , more would like to know how to implement them in php

  • I’m a little confused about what you want, but it’s a good option @Marcelobonifazio gave you. I would suggest something more complex, using an order table, one for meals and one for drink and create their relationship.

1 answer

0

For this example I used jQuery, but can do in javaScript pure with no problem, in case I need to help you.

Decreases the number of test-only items, I changed the value of the elements to an element code, and the text with the amount added the code in the id to pull the correct element reference.

Maybe it’ll help you get started:

function teste() {
  var inputs = [];
  $('input[type=checkbox]').each(function() { // Linha 1
    var verify = (this.checked ? 1 : 0); // linha 2
    if (verify) {
      inputs.push([$(this).val(),$("#"+this.value).val()]); // linha 3
    }
  });
  console.log(inputs);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td colspan="4" width="100">
      <span>Mesa</span>
      <input type="text" id="numero_mesa" name="numero_mesa">
      <label>
        <span>Comanda:</span>
      </label>
      <br>
      <button class="btn" type="button" onclick="teste()">Fazer Pedido</button>
      <br>
    </td>
  </tr>
 
    </tr>
    <tr>
      <td width="280" bgcolor="#00BFFF">Refeição</td>
      <td width="70" bgcolor="#00BFFF">Quantidade</td>
      <td width="150" bgcolor="#1E90FF">Bebida</td>
      <td width="70" bgcolor="#1E90FF">Quantidade</td>
    </tr>

    <tr>
      <td width="280">
        <input type="checkbox" name="pedido_refeicao[]" value="1_1">Costela de Tambaqui sem Espinha</td>
      <td width="70">
        <input type="text" id="1_1" size="7">
      </td>
      <td width="150">
        <input type="checkbox" name="pedido_bebida[]" value="2_1">Fanta Laranja 1l</td>
      <td width="70">
        <input type="text" id="2_1" size="7">
      </td>
    </tr>
    <tr>
      <td width="280">
        <input type="checkbox" name="pedido_refeicao[]" value="1_2">Lombo de Tambaqui Frito sem Espinha</td>
      <td width="70">
        <input type="text" id="1_2" size="7">
      </td>
      <td width="150">
        <input type="checkbox" name="pedido_bebida[]" value="2_2">Fanta Laranja 2l</td>
      <td width="70">
        <input type="text" id="2_2[]" size="7">
      </td>
    </tr>
    <tr>
      <td width="280">
        <input type="checkbox" name="pedido_refeicao[]" value="1_3">Caldeirada de Tambaqui sem Espinha</td>
      <td width="70">
        <input type="text" id="1_3" size="7">
      </td>
      <td width="150">
        <input type="checkbox" name="pedido_bebida[]" value="2_3">Cola Cola 1l</td>
      <td width="70">
        <input type="text" id="2_3" size="7">
      </td>
    </tr>
    <tr>
      <td width="280">
        <input type="checkbox" name="pedido_refeicao[]" value="1_4">Caldeirada de Tucunaré</td>
      <td width="70">
        <input type="text" id="1_4" size="7">
      </td>
      <td width="150">
        <input type="checkbox" name="pedido_bebida[]" value="2_4">Cola Cola 2l</td>
      <td width="70">
        <input type="text" id="2_4" size="7">
      </td>
    </tr>
  </table>

Explaining the code js

  • Line 1: I took all elements checkbox that exist on the form to check those that are or are not marked;
  • Line 2: I check if the element is checked, if I create a auxiliary variable that will help me pull the element;
  • Line 3: I enter the element code and the value that exists in the text with the element id within the array created before line 1.

Testing the code

When you make a request it returns an array more or less like this:

[
  [
    "1_3",
    "5"
  ],
  [
    "2_4",
    "2"
  ]
]

You can impute this array the way this is in your database, or else make a treatment and create relational tables that link the data of the elements to the request, is your choice what you find best and most convenient.

To send the request to the BD

If you’re using jQuery can use $.ajax thus (there is also a way to do without using jQuery):

Within the function teste():

function teste() {
  var inputs = [];
  $('input[type=checkbox]').each(function() { // Linha 1
    var verify = (this.checked ? 1 : 0); // linha 2
    if (verify) {
      inputs.push([$(this).val(), $("#" + this.value).val()]); // linha 3
    }
  });
  var data = {
    inputs
  };

  jQuery.ajax({
    url: "secao.php",
    type: "POST",
    data: data,
    success: function(returnjson) {
      alert("processou");
    },
    error: function(returnjson) {
      alert("erro");
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td colspan="4" width="100">
      <span>Mesa</span>
      <input type="text" id="numero_mesa" name="numero_mesa">
      <label>
        <span>Comanda:</span>
      </label>
      <br>
      <button class="btn" type="button" onclick="teste()">Fazer Pedido</button>
      <br>
    </td>
  </tr>

  </tr>
  <tr>
    <td width="280" bgcolor="#00BFFF">Refeição</td>
    <td width="70" bgcolor="#00BFFF">Quantidade</td>
    <td width="150" bgcolor="#1E90FF">Bebida</td>
    <td width="70" bgcolor="#1E90FF">Quantidade</td>
  </tr>

  <tr>
    <td width="280">
      <input type="checkbox" name="pedido_refeicao[]" value="1_1">Costela de Tambaqui sem Espinha</td>
    <td width="70">
      <input type="text" id="1_1" size="7">
    </td>
    <td width="150">
      <input type="checkbox" name="pedido_bebida[]" value="2_1">Fanta Laranja 1l</td>
    <td width="70">
      <input type="text" id="2_1" size="7">
    </td>
  </tr>
  <tr>
    <td width="280">
      <input type="checkbox" name="pedido_refeicao[]" value="1_2">Lombo de Tambaqui Frito sem Espinha</td>
    <td width="70">
      <input type="text" id="1_2" size="7">
    </td>
    <td width="150">
      <input type="checkbox" name="pedido_bebida[]" value="2_2">Fanta Laranja 2l</td>
    <td width="70">
      <input type="text" id="2_2[]" size="7">
    </td>
  </tr>
  <tr>
    <td width="280">
      <input type="checkbox" name="pedido_refeicao[]" value="1_3">Caldeirada de Tambaqui sem Espinha</td>
    <td width="70">
      <input type="text" id="1_3" size="7">
    </td>
    <td width="150">
      <input type="checkbox" name="pedido_bebida[]" value="2_3">Cola Cola 1l</td>
    <td width="70">
      <input type="text" id="2_3" size="7">
    </td>
  </tr>
  <tr>
    <td width="280">
      <input type="checkbox" name="pedido_refeicao[]" value="1_4">Caldeirada de Tucunaré</td>
    <td width="70">
      <input type="text" id="1_4" size="7">
    </td>
    <td width="150">
      <input type="checkbox" name="pedido_bebida[]" value="2_4">Cola Cola 2l</td>
    <td width="70">
      <input type="text" id="2_4" size="7">
    </td>
  </tr>
</table>

  • it is not working on localhost , type it does not send to the database .

  • How you make the PHP call?

  • I edited the question and put the php code

  • @allanaraujo you put exactly the same code....

  • php code is at the beginning and I put your code in my application but it neither requests nor sends to the database .

  • @allanaraujo editei com a modo como pode enviar os dados pro php

  • When I sent it will no longer appear anything in the database only the id , the selected fields does not appear , as if it were empty

  • the way I had done was going only instead of appearing the order names and the amount it appeared only written array in the database .

  • how I could query to insert this data into mysql ?

Show 5 more comments

Browser other questions tagged

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