Doubt with checkbox and PHP

Asked

Viewed 171 times

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: I need to create a field for each checkbox in the database or can I save all results together in a single field, such as an array? In case you later create a change form using Ajax, I can correctly mark the checkboxes searching the data in the database?

  • 2

    For example, N of these checkboxes are then associated with N users, so create an associative table that will have the user id and the checkbox id. Do not use fields that contain several values that give the first form standard (1FN), a column for each checkbox already imagined if the number increases a lot? already have idea of how to separate the values for the right columns?

  • You can use serialize($listingDizeres) and save to the data field, right? This won’t be useful if you require a future SELECT based on this.

  • In fact each check will be associated with a project, but the relation will be yes N to N. This idea of creating a table part seemed to me the best solution. There will always be these 12 fields (6 for each). @lnkeliz, how does this serialize work?

  • The part table is best option (or a column for each item, which can also be an option, but not the best one). Serialize will exactly turn the entire array into a string, summarizing. It will allow you to send the entire array "as is" directly to the database, you can read in the future using unserialize ($mysql['column']). The problem this method is several, especially if you intend to use a "WHERE sayings = "Arabe" or "JOIN..." will be literally impossible, but it is precisely the option of "I can record all results together in a single field".

1 answer

3


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

I wouldn’t do either: I would create a table A containing 3 fields: id, key and value:

  • The id is self generated;

  • To key is what goes on your value="" of the input type;

  • And the 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 has selected, create a table that connects user with table A and play the ids there =D

It is the best method to be used by own experience in the company in which I work, I’ve had this problem.

In case you later create a change form using Ajax, I can correctly mark the checkboxes searching the data in the database?

Yes, no problem as long as you select in the correct bank.

  • Got it, I’ll do it this way! Thank you very much.

Browser other questions tagged

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