I'm trying to update column values based on checkbox input. The click is triggering the code (I tested with an alert()) but no update is happening in the database and no errors are showing in the console. Please note I'm using images to replace the checkbox.
CSS
input[type=checkbox] {
display: none;
}
label:before {
content: url("../images/house.png");
z-index: 100;
}
:checked + label:before {
content: url("../images/home.png");
}
PHP/HTML
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo
'
<div class="mx-auto" style="max-width:500px;">
<div class="form-group">
<div class="form-inline">
<div class="form-group col-1">
<input type="checkbox" name="home['. $row["id"].']" id="'. $row["id"].'" ' . ($row["home"]==1 ? ' checked="checked"' : '') . '>
<label for="'. $row["id"].'"></label>
</div>
<div class="col-10" style="font-weight:bold;">
'.$row["address"].' '. $row["suburb"].'
</div>
<div class="col-1">
<a style="float:right; margin-bottom:5px;" href="'.$row["gmap"].'" class="btn btn-success">Go</a>
</div>
</div>
AJAX
<script>
$(document).ready(function(){
$('input[type="checkbox"]').on("click", function(){
$.post("work/updateaddress.php",{home:this.checked,id:this.id});
});
});
PHP
$id = $mysqli->real_escape_string($_POST['id']);
$home = $mysqli->escape_string($_POST['home']);
if(isset ($_POST["home"])) {
$sql = "UPDATE addresses SET home='$home' WHERE id='$id'";
if($mysqli->query($sql) === TRUE){
} else {
echo "error" . $sql . "<br>".$mysqli->error;
}
}