Store checkbox value in database

Asked

Viewed 226 times

0

I need your help... I have a custom checkbox, where the user clicks and is "ON" or "OFF" and this value must be saved in the database and must remain checked or unchecked indicated by the funitionnary because through this the admin will know whether the employee is ON (at work or not) or OFF. Turns out it does nothing, does nothing in the database and shows nothing in the browser console.

Follow the HTML code

<form id="checkboxs_data" method="POST" accept-charset="utf-8" >
    <input type="checkbox" style="display: none;" id="checkboxs">
    <input type="hidden" name="checkbox_offon" id="checkbox_offon" value="Off"  />
    <input type="hidden" name="nome_offon" value="<?php echo $row_func['nome'];?>" />
    <input type="hidden" name="id_offon" value="<?php echo $row_func['id'];?>" />


<button type="submit" name="actionhCeckbox" id="actionCheckbox" class="btn btn-primary">
    <span class="glyphicon glyphicon-floppy-saved"></span> 
</button></form>

Input is received by jQuery:

<script>
        $(document).ready(function() {
            $('#checkboxs').checkboxpicker({
                onLabel: 'On',
                offLabel: 'Off'
            });

            $('#checkboxs').change(function(){
                if($(this).is(':checked'))
                  {
                   $('#checkbox_offon').val('Off');
                  }
                else
                  {
                   $('#checkbox_offon').val('On');
                  }
            });

            $('#checkboxs_data').on('submit', function(e){

                e.preventDefault();
                var form_data = jQuery(this).serialize();


                jQuery.ajax({
                    url:"checkboxs.php",
                    method:"POST",
                    data:form_data,
                    success:function(html){
                        $('#checkboxs_data')[0].reset();
                        $('#checkboxs').checkboxpicker('On');
                       setTimeout(function(){ window.location.href = ""; }, 1000); 
                    }
                });
             });


        });
    </script>

And then in turn PHP receives the form and writes to the database

<?php 
   $panel_atual = "funcionario";  $data = date("d-m-Y H:s");

$id_offon = $_POST["id_offon"]; 
$nome_offon = $_POST["nome_offon"];
$checkbox_offon = $_POST["checkbox_offon"];

$connect = "UPDATE funcionarios SET offon = '$checkbox_offon' ,  data = 
'$data' WHERE id = '$id_offon'";
$query = mysqli_query($conexao, $connect) ;

if($query)
{
  echo "<script laguange='javascript'> window.alert('done');</script>";
}else{echo "<script laguange='javascript'> window.alert('error'); 
 </script>";}

?>

This is the checkbox button with bootstrap:

inserir a descrição da imagem aqui

  • $.post( "test.php", { name: "John", time: "2pm" }) . Success(Function( data ) { Alert( "Data Loaded: " + data ); });

  • I’ve been talking about those quotes

  • if($query){ echo json_encode(["msg" => 'successfully saved parabens',"data" => 0]); }Else{ echo json_encode(["msg" => 'error not saved',"data" => 0]); }

  • Ohh pah... I forgot to put the connection that comes from config require ".. /config.php";

  • But it still doesn’t work... I’m sorry I didn’t quite understand where @dvd says to remove if(jQuery)

  • I’ve already remade the answer for you to 4 comments to top @Josepinto

  • It was already in the code... when posting I forgot to put it

  • 1

    Delete the past comments!

  • I will check here... Thank you very much for the personal help... In case I get no return

  • Hi, Everybody! I was able to do it as you spoke by the code $('#checkboxs_data'). Submit(Function(e) { e.preventDefault(); $.post( "checkboxs.php", $( "#checkboxs_data" ).serialize() . done(Function(Response) {$('#checkboxs'). checkboxpicker('On'); swal('Saved changes', Response.message, Response.status); }); });

  • But how can I keep the "Checkbox" on... because when I refresh the page it goes back to the off.. I tried using "if" for value in the database for ex. if you have written On it keeps Input with checked otherwise keep as unchecked even then it didn’t work... Can you help me, please?

Show 6 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.