How to save the checkbox options in the bank?

5

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

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>
    
asked by anonymous 14.12.2014 / 06:21

1 answer

1

Both CheckBox fields and RadioButton fields 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 by submitting on form:

Explaining...IsendinarrayformanddotheprocessinginPHPasbelow...

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 on the bank is at your discretion ....

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

    
16.12.2014 / 14:10