3

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");
?>

2 Answers 2

5

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

}
Sign up to request clarification or add additional context in comments.

4 Comments

If i change the Status of the button how do i save the value?and could u explain what that '?' is doing between 1 and checked?
@BrunoGonçalves Updated answer. The checkbox will not send '1' when the form is submitted and will send nothing then its unchecked.
so what variable i receive on the file that connects with database? For example i have to have this: $something=$_POSt['what i put here?'].
@BrunoGonçalves Updated answer again.
1

Try this one ...write update query in uppdatestatus.php file

 <input type="checkbox" id="<?=$row['id']?>"  name="onoffswitch" value="<?=$row['status']?>" class="js-switch" <?=$row['status'] == '1' ? 'checked' : '' ;?>/> 

$('input[name=onoffswitch]').click(function(){
var id=$(this).attr('id');
var status = $(this).val();
if(status == 1) {
    status = 0; 
} else {
    status = 1; 
}
//alert(id);
$.ajax({
        type:'POST',
        url:'updateustaus.php',
        data:'id= ' + id + '&status='+status
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.