This question has probably been asked multiple times but I still cannot figure out a proper working solution to my problem. So, I have an sql table (id-> int and status-> tiny-int) which needs to be activated without submit button and retain their current state either on or off. I found the solution to my problem here. But, this is only for one checkbox and I need it to be for four checkboxes. My attempt at it sorta works but I feel there is probably a better way of doing it. Can someone please provide me with any suggestions?
thanks
<form id="form" action="" method="POST" >
<input type="checkbox" name="checkbox1" onchange="$('#form').submit();"
<?php if(isset($_POST['checkbox1'])) { echo 'checked="checked"'; } ?>
<input type="checkbox" name="checkbox2" onchange="$('#form').submit();"
<?php if(isset($_POST['checkbox2'])) { echo 'checked="checked"'; } ?>>
</form>
if (isset($_POST['checkbox1'])) {
$sql1="UPDATE switch SET status = '1' WHERE id = '1'";
} else {
$sql1="UPDATE switch SET status = '0' WHERE id = '1'";
}
$result=$conn->query($sql1);
if (isset($_POST['checkbox2'])) {
$sql2="UPDATE switch SET status = '1' WHERE id = '2'";
} else {
$sql2="UPDATE switch SET status = '0' WHERE id = '2'";
}
$result=$conn->query($sql2);
>at the end