Doubt with checkbox and PHP

2

I have 2 fields, as below, which are checkboxes :

              <tr>
              <td>Dizeres Legais:</td>
              <td><input type="checkbox" name="export_dizeres[]" value="PORTUGUES"> Português
                  <input type="checkbox" name="export_dizeres[]" value="INGLES"> Inglês
                  <input type="checkbox" name="export_dizeres[]" value="ESPANHOL"> Espanhol
                  <input type="checkbox" name="export_dizeres[]" value="FRANCES"> Francês
                  <input type="checkbox" name="export_dizeres[]" value="ARABE"> Árabe
                  <input type="checkbox" name="export_dizeres[]" value="COREANO"> Coreano</td>
          </tr>

          <tr>
              <td>Tabela Nutricional:</td>
              <td><input type="checkbox" name="export_tabela[]" value="PORTUGUES"> Português
                  <input type="checkbox" name="export_tabela[]" value="INGLES"> Inglês
                  <input type="checkbox" name="export_tabela[]" value="ESPANHOL"> Espanhol
                  <input type="checkbox" name="export_tabela[]" value="FRANCES"> Francês
                  <input type="checkbox" name="export_tabela[]" value="ARABE"> Árabe
                  <input type="checkbox" name="export_tabela[]" value="COREANO"> Coreano</td>
          </tr>

Step them into PHP, I can display them as follows:

$listaDizeres = $_POST['export_dizeres'];

foreach ($listaDizeres as $export_dizeres) {
    echo $export_dizeres.'<br>';

}
$listaTabela = $_POST['export_tabela'];

foreach ($listaTabela as $export_tabela) {
    echo $export_tabela.'<br>';

}

My question is this: Do I need to create a field for every checkbox in the database, or can I write all the results together in a single field, such as an array? If I subsequently create a change form using Ajax, can I correctly mark the checkboxes by searching the data in the database?

    
asked by anonymous 08.01.2016 / 19:33

1 answer

3
  

I need to create a field for each checkbox in the bank or I can record   all result together in a single field, like an array?

I would not do either: I would create a table A that contains 3 fields: id, key and value:

    The id is self-generated;

    The key is what goes into your input type value="" ;

  • And value is the checkbox text .

If you want to simplify, you can take the key from the table and constitute the value="" attribute of the table id.

So, to register what the user selected, create a table that connects user with table A and throw the ids there = D

It's the best method to use for my own experience in the company I work for, I've had this problem.

  

If I subsequently create a change form using Ajax, can I correctly check the checkboxes for the data in the database?

Yes, with no problem since you select in the correct database.

    
08.01.2016 / 21:21