Receive data from a PHP checkbox

0

It is possible to capture a MySQL table data in a form with a checkbox as if it were Boolean, that is, I need to capture the following:

Nombre de empleado
Tiene Deuda   Si _ No _ 

But this part I do not find how to pass it to the insertion part of data in the database.

This is the code I have in HTML:

<input type="checkbox" name="Prestamos" id="Prestamos" value="1"> 

With this one I already receive it:

<?php

   if (isset($_POST['Prestamos']) && $_POST['Prestamos'] == '1')
      echo '<div style="color:green">El empleado tiene deudas</div>';
   else
      echo '<div style="color:red">El empleado no tiene deudas.</div>';

?>
    
asked by franksegovia 05.06.2018 в 19:12
source

2 answers

0

if you can. I'm going to assume that you have an index.html where you have the form and you're going to send it to guardarDeuda.php (I do not know how you called them) and that you are using myslqi .

Try something like this

<h1>Tiene deuda</h1>
<form method="guardarDeuda.php" action="post">
<label for="primerOpcion">Si</label>
<input type="checkbox" value="1" name="Prestamos" id="primerOpcion">
<label for="segundaOpcion">No</label>
<input type="checkbox" value="0" name="Prestamos" id="segundaOpcion">
<input type="submit" value="Enviar" name="enviar" />
</form>

in the file guardarDeuda.php ..

<?php
include('conexion.php');
   if (isset($_POST['enviar'])){
$deudas =$_POST['Prestamos'];
if($deudas){
echo "TIENE DEUDAS";
}else{
echo "NO TIENE DEUDAS";
}
$conexion->query("INSERT INTO deudores (deudas) values('$deudas')");
}
header("Location:index.html");
      exit();
     ?>

file conexion.php

<?php
$s = "localhost";
$bd = ""; // nombre de base de datos
$u = "root"; //usuario
$p = ""; //contraseña

$conexion = new mysqli($s, $u, $p, $bd);
if ($conexion->connect_errno) {
    echo "no conectado";
}


?>

Now you only have to save the variable $ debts in your database, remember that 1 saves it as true and false == 0. I hope it helps you. greetings

    
answered by 05.06.2018 / 19:30
source
0

I think I did not understand the question very well, you mean next to sql?

Depending on which Mysql server you are using You can use the datatype BIT in your insert. So you already use values like 0 and 1 for your boolean.

You can also use the Datatype TINYINT()

Where TINYINT(0) is equivalent to FALSE and TINYINT(1) is equivalent to TRUE

True and False of TINYINT() is exactly the equivalent in BIT are only 0 and 1

mysql> SELECT IF(0, 'true', 'false');
+------------------------+
| IF(0, 'true', 'false') |
+------------------------+
| false                  |
+------------------------+

mysql> SELECT IF(1, 'true', 'false');
+------------------------+
| IF(1, 'true', 'false') |
+------------------------+
| true                   |
+------------------------+

mysql> SELECT IF(2, 'true', 'false');
+------------------------+
| IF(2, 'true', 'false') |
+------------------------+
| true                   |
+------------------------+

I leave you as a source the documentation of MySQL Documentation of DataTypes

    
answered by 05.06.2018 в 19:27