Here is the HTML / JavaScript:
<form method = "post" style="display: inline;" name="thisform<?php echo $employee->id; ?>"> <input type="hidden" name="user" value="<?php echo $employee->id; ?>" /> <input type="hidden" name="auto" value="<?php echo $employee->stay_live; ?>" /> <h3><input type="checkbox" name="stay_live" onclick="document.forms.thisform<?php echo $employee->id; ?>.submit();" <?php if ($employee->stay_live == '1') { echo "checked"; } ?> title="Click to Stay Live Every Day" /> <?php echo $employee->name; ?> </form>
Here is the PHP :
<?php if(isset($_POST['stay_live'])) { $user=$_POST['user']; $auto=$_POST['auto']; if ($auto == 1) { $sql = "UPDATE users SET stay_live = 0 WHERE id = '{$user}';"; } if ($auto != 1) { $sql = "UPDATE users SET stay_live = 1 WHERE id = '{$user}';"; } mysqli_query($dbconnection, $sql); } ?>
If you see onclick code on the form, it will automatically submit the form and do the PHP POST . However, onclick only works if the checkbox NOT installed. If it is installed, the screen refreshes as if it were doing something, but the POST was never sent.
So, we summarize; everything works as expected when the box is unchecked and needs to be checked. But for UNSET to do this, the server function is never called. Can anyone understand why?
Note: this is test code, so please, no comments on SQL injection
source share