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:
$.post( "test.php", { name: "John", time: "2pm" }) . Success(Function( data ) { Alert( "Data Loaded: " + data ); });
– Willian
I’ve been talking about those quotes
– Willian
if($query){ echo json_encode(["msg" => 'successfully saved parabens',"data" => 0]); }Else{ echo json_encode(["msg" => 'error not saved',"data" => 0]); }
– Willian
Ohh pah... I forgot to put the connection that comes from config require ".. /config.php";
– Jose Pinto
But it still doesn’t work... I’m sorry I didn’t quite understand where @dvd says to remove if(jQuery)
– Jose Pinto
I’ve already remade the answer for you to 4 comments to top @Josepinto
– Willian
It was already in the code... when posting I forgot to put it
– Jose Pinto
Delete the past comments!
– NoobSaibot
I will check here... Thank you very much for the personal help... In case I get no return
– Jose Pinto
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); }); });
– Jose Pinto
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?
– Jose Pinto