How to validate a switch and insert it into the database if the data in my db is Boolean?

2

I have a form to which I enter the data of the staff, but to give it a more elegant appearance in view of the user I have inserted a switch component to 'validate' if that 'character' will be active or inactive, which will be saved as true or false in my mysql database.

The problem would be how to manipulate this component so that when I send the data to my DB validate if it is true or false and save it?

The only code I have is this, because that's where I should start but I would not know how, obviously it's the first time I use it ...

  <div class="switch">
    <label>
      Off
      <input type="checkbox">
      <span class="lever"></span>
      On
    </label>
  </div>

I would appreciate your cooperation.

    
asked by JDavid 03.03.2017 в 23:01
source

2 answers

2

In order for the backend to receive something, your checkbox must be in the checked state and it must have a name . If you do not set attribute value , by default it will send the value on when it is checked. But to simplify things, let's put value 1.

<input type="checkbox" name="micampo" value="1">

When checked, the backend receives 1. When it is not checked, the backend receives nothing, as if the checkbox did not exist.

Therefore, the most common technique is to put an input of type hidden with the same name, which if the checkbox is deactivated, that input is responsible for sending a zero, false or off to the backend.

<input type="hidden" name="micampo" value="0"/>
<input type="checkbox" name="micampo" value="1"/>

This can be proven in the example below. Take into account that in that serialization, the last one that declares the value of a name is the one that is valid, so that

backend.php?micampo=0&micampo=1

Equivalent to

backend.php?micampo=1

jQuery('#comprobar').on('click',function() {
  console.log('Voy a enviar',jQuery('#miformulario').serialize());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="miformulario">
<input type="hidden" name="micampo" value="0"/>
<input type="checkbox" name="micampo" value="1"/>
<input type="button" id="comprobar" value="comprobar"/>
</form>

Regarding the "component" the switch is really just a cosmetic effect that adds materialize. Your checkbox is still as it is, but invisible.

PS: this can obviously be done without jQuery, but materialize by itself has jQuery as dependency.

    
answered by 03.03.2017 / 23:26
source
1

For a case you enter in the table a value 0 or 1 depending on whether it is active or not.

Example:

<?php
    if(isset($_POST["check"])){
        echo "esta ok";
    }else{
        echo "no esta ok";
    }
?>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form action="index.php" method="post">
            <input type="checkbox" name="check" />
            <input type="submit" name="enviar" />
        </form>
    </body>
</html>
    
answered by 03.03.2017 в 23:35