How to store multiple checkboxes

1

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

  

if (! empty ($ _ POST ['gender']) & count ($ _ POST ['gender'])       $ items = implode (',', $ _POST ['gender']);

this is 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);

}
?>

<fieldset><divclass="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>
    
asked by anonymous 19.01.2017 / 01:24

1 answer

0

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

Decrease the number of items for testing only, I changed the value of the elements to an element code, and the text with the quantity added the code in the id to pull the reference from the correct element.

Maybe it helps 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><tdcolspan="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 get all checkbox elements that exist in the form to check what is or is not checked;
  • Line 2 : I check if the element is checked, if 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 inside the array created before line 1.

Testing the code

When you place an order it returns an array something like this:

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

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

To send the request to the DB

If you are using jQuery you can use $.ajax so (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><tdcolspan="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>
    
19.01.2017 / 02:06