Multiple checkbox and save to bank

Asked

Viewed 6,584 times

2

I have a form on which I will be able to mark several checkbox at the same time and wanted to know how I identify those marked and save them in the database.

On the bench I’ll have a table and a column for each checkbox because this is a record of a consultation, and if the person is going to do several treatments, those that are marked will give a OK in that column in the database.

For example if the person is going to have Reiki and of Acupuncture, there’s gonna be a OK in Reiki and a 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 didn’t put all the code, but after that if I click register, it calls the page that will insert the data in the database. There will be a column for each option and if it is checked, it will store one OK.

If anyone knows how to do that and can help me, I’d appreciate it.

  • What’s the problem? Wouldn’t it be better to store checkboxes in a separate table? e.g., query and itens_query ?

  • I need to save what the person will do for treatment because then I will show this data in the general report and still have the days of presence. each day of the week is an intact treatment who have doing Iki I will list people in Iki.

  • Puts the table structure

2 answers

3


To do this, you only need to assign indexes to those(s) array, still in the form HTML.

<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 the PHP just read the values 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 still have some questions, make some modifications to HTML and uses the print_r($_POST) to see what values have been sent, and how they are identified or organized in this array.

0

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

An example in code. Assuming I have 4 options that can be selected individually or simultaneously: student, employee, voter, disabled I assign to each, 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 of them just add it to the total Ex.: To assign employee, voter and disabled I add 2+4+8 = 14 When displaying marked options I use the & (Commercial or Ampersand) operator 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 only want to recover an HTML information and save in the database use the same common way.

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

In PHP will recover by 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 to BIT operator: As I explained, Voce can have as many options as you want a simple code to solve, just by saving the sum in a single field in the database and having a table only with the services you want to appear as an option. Having a column for each service is the wrong way to do.

  • I need to know how to capture the checkbox and save in the bank that you did I would have to set up dozens of conditions and that’s not it. i just want to take the marked q ta and give an ok in the respective column in the bank.

  • For your HTML it would be something like $todos = isset($_REQUEST['check']) ? (array) $_REQUEST['check'] ? array(); if (isset($todos['kr'])) { // marcou Reiki

  • look it’s like this. I’ll register the date of the person’s consultation. all treatments q she form save I have to save No bank. in the database will have a table with several columns pq each registration will have an id. take the amount of marked options does not serve. Because I need to mark on the bench in the column of Iki whether or not she will do, in the acupuncture column whether or not she will do.

  • 1

    Yeah, what I passed on in the commentary is what you need to do. In your HTML Voce is using the same name for all checkbox fields added from [] which will make PHP convert to an array where each index will be the field name.

  • intao é q para monta I understood that I had to do as an array but until now I didn’t understand how I capture the marked and saved in the right place in the bank and your messages I saw after that I replied

  • when a field is marked, it is sent in the form of name=value to the page As your fields have all the same Ames (check[]) it will be sent as check[]=value. In PHP, you have to check whether or not it was marked like this: $rk = isset($_REQUEST['chek']['rk']); If $rk is different from fake it is because it has been marked and then you have to continue writing the code to mark the Bank field.

  • got Aki worth

  • @Marcosregis how could I resolve this my http://answall.com/questions/178626/como-usar-o-implode-array-to-string

Show 3 more comments

Browser other questions tagged

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