Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage data using switch/toggle button on html with php

Tags:

html

database

php

I have one switch/toggle button that change the check status according to the data that i receive from databse.And if it's ON and if I click i want to save in a variable the value 0, and if i want to put ON i save 1.

The html code is this:

<div class="d5 col-md-2 col-xs-6">
        <?php if($ligado=='0'){
          ?><input id='cmn-toggle-1' class='cmn-toggle cmn-toggle-round' onclick="$ligado=1"type='checkbox'unchecked >
        <label for='cmn-toggle-1'></label>
      <?php }
        else{
          ?>
          <input id='cmn-toggle-1' class='cmn-toggle cmn-toggle-round' onclick="$ligado=0" type='checkbox'checked >
        <label for='cmn-toggle-1'></label>
        <?php
        }?>
      </div>

This is inside of a form and when i POST i want to send the variable with the right value. I dont know if with onClick works, because i tested and i get nothing.

In my php i have this:

    <?php
include_once 'dbconfig.php';
$descricao=$_GET['descricao'];

if(!empty($_POST['my_checkbox'])) {

$sql_query = "UPDATE alarme SET ligado=1 WHERE divisao=(SELECT id from    divisao where descricao = '$descricao');";
$result_set=mysqli_query($link, $sql_query);

} else {

$sql_query = "UPDATE alarme SET ligado=0 WHERE divisao=(SELECT id from divisao where descricao = '$descricao');";
$result_set=mysqli_query($link, $sql_query);

}
header("Location:verdispositivo.php");
?>
like image 685
Bruno Gonçalves Avatar asked Mar 04 '26 16:03

Bruno Gonçalves


1 Answers

You are trying to use a php variable in Javascript.

Try this:

<input  id='cmn-toggle-1' 
        class='cmn-toggle cmn-toggle-round'
        type='checkbox' 
        value='1'
        name='my_checkbox'
        <?php echo $ligado== '1' ? ' checked' : ''; ?> >
<label for='cmn-toggle-1'></label>

On submission you can check for the variable:

<?php

if(!empty($_POST['my_checkbox'])) {

    // user checked the box

} else {

    // user did not check the box

}
like image 181
Angad Dubey Avatar answered Mar 06 '26 07:03

Angad Dubey