How to save checkbox options in the bank?

Asked

Viewed 4,666 times

6

I need to save the fruits and values in different fields in the bank"

PHP:

<?php
   session_start();
   //if (!isset($_SESSION["usuario"])){echo "<script>alert('O SEU INGRESSO É VALIDO');window.location=\"index.php\";</script>";}
   include "funciones.php";
   date_default_timezone_set("America/Sao_Paulo");

   if (isset($_POST["edit_fruta_x"])){

      $_SESSION["idrelatorio"]=$_POST["relatorio"];//Se o usuário escolheu da lista um projeto     para diagnosticar, guardo o seu IDprojeto para referenciar
      $_SESSION["idbanca"]=$_POST["banca"];
      $_SESSION["idfruta"]=$_POST["fruta"];

      unset($_POST["fruta"]);

      lerbanca($_SESSION['idbanca']);//procura os dados da banca escolhida
      lerlocalizacao($_SESSION["idrelatorio"]);
   }

   else{
      $_SESSION["idrelatorio"]=$_POST["relatorio"];
      $_SESSION["idbanca"]=$_POST["banca"];

      lerlocalizacao($_SESSION["idrelatorio"]);
      unset($_SESSION['idfruta']);
      unset($_SESSION['fruta']);
   }

?>

HTML:

<script type="text/javascript">
   }
</script>
</head>
<body>

<form class="form-signin" action='salvafrutas.php' method="post">

<label>bancas de frutas</label>
<!-- 1.1 salvar frutas   -->
<label>1. Lista de Frutas</label>

<table>
<!-- lista que seleciona as ações do botão para saber se está marcado ou não-->
<tr><th colspan=3><b>1.Tipo de frutas</b></th></tr>
<tr><th colspan=2>Características</td><th colspan=2>valores</th></tr>
<tr><td colspan=2>1. uva</td><td><input id="uva" type="checkbox"     name="comportamento[]" value="6"  <?php if(isset($_SESSION['comportamento'])){echo (strpos($_SESSION['comportamento'],'6')!==false) ? 'checked="checked"' : '';}?> onclick="apaga_nao1(this)"> 6 </td></tr>
<tr><td colspan=2>2. banana</td><td><input id="banana" type="checkbox" name="comportamento[]" value="0" <?php if(isset($_SESSION['comportamento'])){echo (strpos($_SESSION['comportamento'],'0')!==false) ? 'checked="checked"' : '';}?> onclick="apaga_nao1(this)"> 0 </td></tr>
<tr><td colspan=2>3. maça</td><td><input id="maça" type="checkbox" name="comportamento[]" value="3" <?php if(isset($_SESSION['comportamento'])){echo (strpos($_SESSION['comportamento'],'3')!==false) ? 'checked="checked"' : '';}?> onclick="apaga_nao1(this)"> 3 </td></tr>
<tr><td colspan=2>4. pera</td><td><input id="pera" type="checkbox" name="comportamento[]" value="4" <?php if(isset($_SESSION['comportamento'])){echo (strpos($_SESSION['comportamento'],'')!==false) ? 'checked="checked"' : '';}?> onclick="apaga_nao1(this)"> 4 </td> </tr>
</table>
<br>
<!--<button class="btn btn-lg btn-primary btn-block" type="submit">Salvar</button> -->  
<input id='btgravar' type='image' src="salvar.png" title='SALVAR' width='70' height='70'/>
</form>
</body>
</html>
  • 2

    Good, it was clearer! I’ve already given my vote to reopen. What do you want to save in the comic? When you have in HTML name="comportamento[]" this will cause PHP to reach an array where it has values from value HTML. That is, if someone chooses all the checkboxes they will have something like array(6, 0, 3,4);, is that what you want to keep in the comic book or do you want the name of the fruit too? You can explain better all the information you want to save in the comic book of that form?

  • You need to make an array with the name of the fruits, linked to the code you assigned in the value of your select html. Example: $fruits = array(0=>"banana", 3=>"maca", 4=>"pera", 6=>"grape"); $nomedafruta = $fruits[$_POST["behavior"]; (just example, it is necessary to foreach $_POST["behavior"] in order to get all the marked...)

  • So @Marianagomes - Inside the foreach, if you’ve done the $fruit array like I said (declare at the beginning of your php), you’ll have the fruit with $fruit[$value]... If you want content separated by /, you need to do: $comporta['comportamento'] .= $value.":". $fruit[$value]." /"; Thus Voce will have the selected number, two points (:), and the name of the fruit, followed by a bar (/).

  • Good morning Mariana, can you answer my question/comment above?

  • @Sergio, I need to save the fruits and values, but in different fields in the database.

1 answer

2

So many fields CheckBox how many fields RadioButton serve the same purpose, I have an example that I do as follows:

HTML:

DES2 <input type="checkbox" name="motivo[]" id="DES2" onclick="checar(this.id)" value="0" />
DES3 <input type="checkbox" name="motivo[]" id="DES3" onclick="checar(this.id)" value="0" />
DES4 <input type="checkbox" name="motivo[]" id="DES4" onclick="checar(this.id)" value="0" />

JS:

function checar(idObj) {
    val = idObj;
    idObj = "#" + idObj;
    if ($(idObj).is(':checked')) {
        $(idObj).val(val);
    } else {
        $(idObj).val(0);
    }
}

How it is sent when giving Ubmit in the form:

inserir a descrição da imagem aqui

Explaining... I send in array form and do the processing in PHP as follows:...

PHP:

    $_POST["DES2"] = 0;
    $_POST["DES3"] = 0;
    $_POST["DES4"] = 0;

    $checkBox = $_POST['motivo'];
    if ($checkBox) {
        $i = 0;
        foreach ( $checkBox as $value ) {
            switch ($value) {
                case "DES2" :
                    $_POST["DES2"] = 1;
                    break;
                case "DES3" :
                    $_POST["DES3"] = 1;
                    break;
                case "DES4" :
                    $_POST["DES4"] = 1;
                    break;
                default :break;
                $i++;
            }
        }
    }

The way you save to the bank is up to you....

There are other ways to do this, sometimes even easier... but it depends on your implementation, the standards of the company and Talz

Browser other questions tagged

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