0

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;
    }
  }
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Anthony Truong
  • 53
  • 2
  • 12
  • Did you check by printing the value of $_POST in your php code? Is it empty? – SanketR May 16 '18 at 08:20
  • 1
    Possible duplicate of [How to update Database while checking checkbox with jquery?](https://stackoverflow.com/questions/36140045/how-to-update-database-while-checking-checkbox-with-jquery) – Pradeep May 16 '18 at 08:20
  • Your reference in the HTML to the 'home' is not 'home' its 'home[#]' replacing # with whatever row['id'] is. So your server side PHP needs to be changed to reference the correct home element. Your PHP is a bit odd, why are you checking for isset($_POST['home']) after you've tried to use it? – SPlatten May 16 '18 at 08:23
  • Add print_r($_POST); in your PHP that will dump the contents of $_POST in the console so you can review. – SPlatten May 16 '18 at 08:25
  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) in either the `MYSQLI_` or `PDO` API's – RiggsFolly May 16 '18 at 08:28
  • Please share Post data in your ajax request by print_r($_POST) OR you can find the same in console/networks under parameter tab. – vijaybir singh May 16 '18 at 08:28

1 Answers1

0

You need to access the POST-variable in this way

$_POST['home'][$_POST['id']]

because you have defined the name like

<input type="checkbox" name="home[id]">

further i suggest you to check the id and the checkbox-status like this:

$id   = intval($_POST['id']);
$home = ($_POST['home'][$_POST['id']] == 'true' ? 1 : 0);
Bernhard
  • 1,852
  • 11
  • 19