I am trying to get the value (true or false) of a checkbox and update the database of a row in a while function. However, the checkboxes only seem to register a change of value depending on the first row. For example, if row 1 checkbox is checked, value = true. I can then click on the following rows and get checkbox value of true. However, when I click rows beyond the first row to get false, it will only register if the first row checkbox was unchecked first. I think it's something to do with the row id but tried for ages to fixed this but can't fix it. Please help.
HTML/PHP
while($row = $result->fetch_assoc()) {
echo
'
<input type="checkbox" name="home['.$row["id"].']" id="'.$row["id"].'" '.($row["home"]>0 ? 'checked="checked"' : '').'>
<label for="'. $row["id"].'"></label>
JQUERY
$(document).ready(function(){
$('input[type="checkbox"]').on("click", function(){
var on = $('input[type="checkbox"]').prop("checked");
$.post("work/updateaddress.php",{home:on,id:this.id});
});
});
PHP/MYSQL
if ($home == 'true') {
$check = date("Ymd");
} else {
$check = '';
}
if(isset($_POST["home"])) {
$sql = "UPDATE addresses SET home='$check' WHERE id='$id'";
if($mysqli->query($sql) === TRUE){
} else {
echo "error" . $sql . "<br>".$mysqli->error;
}
}