Checkbox multiple and save in bank

2

I have a form where I can mark several checkbox at the same time and wanted to know how to identify the ones that are marked and save them in the database.

In the database I will have a table and a column for every checkbox because this is a record of a query, and if the person is to make multiple treatments, those that are marked will give a OK in that column in the database of data.

For example, if the person is to treat Reiki and Acupuncture , a OK will appear in Reiki and OK in Acupuncture.

<form name="signup" method="post" action="../cadastro_con.php">
  <div>
  
  <form name="signup" method="post" action="cadastrar.php">
  <div align="center">
    <table width="800" border="1" bordercolor="#B9B3B3" cellspacing=0 cellpadding=0>
      <tbody>
        <tr>
          <td><p><br>
            NOME:
            <input type="text" name="nomec" size=50/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DATA DA CONSULTA:
            <input name="data" type="date" />
            <br>
          </p>
            <table width="600" border="0" bordercolor="#B9B3B3" cellspacing="0" cellpadding="0">
              <tbody>
                <tr>
                  <td width="185" height="86" align="center" bordercolor="#FFFFFF" bgcolor="#DDF0DD" style="border-style:none" >TRATAMENTOS A SEREM REALIZADOS</td>
                  <td width="256"  style="border-right-style:none">
                  <input type="checkbox" name="check[]" value="db">
                  <label>Desobsessao e Desmaterializa&ccedil;&atilde;o
                  </label><br>
                  <label>
                      <input type="checkbox" name="check[]" value="cr">
                      Cirurgias </label><br>
                      <label>
                      <input type="checkbox" name="check[]" value="rk">
                      Reiki
                    </label></td>
                      
                  <td width="114" style="border-left-style:none">
                  <input type="checkbox" name="check[]" value="crm">
                    <label >Cromoterapia
                    </label><br>
                    <label>
                      <input type="checkbox" name="check[]" value="acp">
                      Acupuntura
                      <br>
                     </label>
                     <label>
                      <input type="checkbox" name="check[]" value="pdc">
                      Passe de Cura
                    </label></td>
                  </tr>
              </tbody>
        </table>
            </form>

I did not put the whole code, but after that if I click on register, it calls the page that will enter the data in the database. There will be a column for each option and if it is checked, it will store a OK .

If anyone knows how to do this and can help me, thank you.

    
asked by anonymous 09.10.2015 / 00:10

2 answers

3

To do this, you only have to assign indexes to these array, even in the HTML form.

<div class="info"><?php echo isset($msg) ? "<h2>Retorno:</h2>" . $msg . "<hr>" : NULL; ?></div>
        <h2>Formulário:</h2>
        <form method="POST" action="">
            <input type="text" name="nome" placeholder="Digite o seu nome"/><br/><br/>
            <h3>Trabalhou como:</h3>
            <label for="check[med]">Médico(a):
            <input type="checkbox" name="check[med]" value="Médico(a)"/>
            </label><br/>
            <label for="check[mec]"> Mecânico:
                <input type="checkbox" name="check[mec]" value="Mecânico(a)"/>
            </label><br/><br/>
            <input type="submit" name="enviar" value="Enviar"/>
        </form>

And with PHP you just read the values contained in them.

if(isset($_POST["enviar"])){
    $nome = isset($_POST["nome"]) ? (string) $_POST["nome"] : NULL;
    $prof = isset($_POST["check"]) ? $_POST["check"] : NULL;
    if(!empty($prof) && !empty($nome)){ 
        $msg = null;
        $msg .= "<b>Nome:</b> " . $nome . "<br/>";
        $msg .= "<b>Já trabalhou como: </b><br/>";  
        foreach($prof as $val){ 
            $msg .= $val . "<br/>";
        }   
    }   
}

This example is simple and easy to test. If you continue with some questions, make some changes to HTML and use print_r($_POST) to see what values have been sent, and how they are identified or organized in that array.

    
09.10.2015 / 02:34
0

In this case, the use of BIT-to-BIT operators fall like a Glove. For this it is important to understand the concept.

An example code. Assuming I have 4 options that can be selected individually or simultaneously: student, employee, voter, disabled I assign to each one, power value of 2 starting at 0

estudante = 2^0 = 1
empregado = 2^1 = 2
eleitor = 2^2 = 4
deficiente = 2^3 = 8

To assign any one of them just add it to the total Eg: To assign employee, voter and disabled I am 2 + 4 + 8 = 14 When viewing the checked options I use the & (E Commercial or Ampersand) to know if the option is contained in the total

    2 & 14 == 2 // Se o resultado da operação for o próprio valor ele está contido
    1 & 14 == 1 // Nesse caso, como 1 não está contido no total, o resultado será 0

So, just adapt this rule to your need.

If you just want to retrieve an HTML information and save to the database use the same common form.

    <input type="checkbox" value="1" name="depilacao" />

In PHP it will recover by the NAME attribute

    $depilacao = isset($_REQUEST['depilacao']) ? (int) $_REQUEST['depilacao'] : null;
    // ...
    if ($depilacao > 0 ) // Marcou a opção depilação

Just clarifying the comment that said it would have many conditions using BIT operator to BIT: As I explained, you can have as many options as you want a simple code will solve by saving the sum in a single field in the database and have a table with only the services that you want to appear as an option. Having a column for each service is the wrong way to do it.

    
09.10.2015 / 00:30