Boky
Boky

Reputation: 12094

how to get unchecked checkboxes

I have next code :

<div class=\"col-sm-1 col-xs-1\" style=\"padding:9px 0 0 0;text-align:right;\">
 <input type='hidden' value='0' name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][]\">
 <input type=\"checkbox\" name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][]\" id=\"chcWeek\" value=\"1\" checked>
</div> 

On my next page where I need to get values of a checkboxes I have next :

foreach ($_POST["chcWeek"] as $orderDetId => $detOrderId) {
  foreach ($detOrderId as $prodId => $idProd) {
    foreach($idProd as $checked){
        echo "Za product " . $prodId . " je checkbox vrijednost : " . $checked . "<br />";
        if($checked == "on"){
            //uzima product_shop_tt_id iz order_details
            $rezProdShopttID = mysqli_query($kon, "SELECT * FROM order_details WHERE id = ". $orderDetId ." LIMIT 1");
            $redProdShopttID = mysqli_fetch_assoc($rezProdShopttID);
            $prod_shop_tt_id = $redProdShopttID["product_shop_tt_id"];
            //Postoji li
            $rezProvjera = mysqli_query($kon, "SELECT * FROM weekelijks WHERE product_shop_tt_id = ". $prod_shop_tt_id ." AND user_id = ". $user_id ." LIMIT 1");
            $brProvjera = mysqli_num_rows($rezProvjera);
            if($brProvjera < 1){
                mysqli_query($kon, "INSERT INTO weekelijks VALUES (NULL, ". $user_id .", ". $prod_shop_tt_id .", 0)");  
            }
        }else{
            $rezProvjera = mysqli_query($kon, "SELECT * FROM weekelijks WHERE product_shop_tt_id = ". $prod_shop_tt_id ." AND user_id = ". $user_id ." LIMIT 1");
            $brProvjera = mysqli_num_rows($rezProvjera);
            if($brProvjera > 0){
                mysqli_query($kon, "DELETE FROM weekelijks WHERE user_id = ". $user_id ." AND prod_shop_tt_id = ". $prod_shop_tt_id ."");   
            }
        }
    }
  }
}

As result I get :

enter image description here

Thus for product which has been checked I get two values (0 and 1) and for those which hasn't been checked I get values 0. How can I for those which has been checked get only value 1?

How can I in my code write that if the checkbox has been checked insert that product into a database and if hasn't been checked delete it form the database?

Thanks in advance.

Upvotes: 0

Views: 60

Answers (1)

tzunghaor
tzunghaor

Reputation: 1035

Non checked checkboxes are not submitted at all.

To be on the safe side, I would explicitly add indexes to distinguish the two inputs:

<div class=\"col-sm-1 col-xs-1\" style=\"padding:9px 0 0 0;text-align:right;\">
 <input type='hidden' value='0' name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][hidden]\">
 <input type=\"checkbox\" name=\"chcWeek[". $red["orderDetID"] ."][". $red["prodID"] ."][checkbox]\" id=\"chcWeek\" value=\"1\" checked>
</div> 

And in the PHP:

foreach ($_POST["chcWeek"] as $orderDetId => $detOrderId) {
  foreach ($detOrderId as $prodId => $idProd) {
    if(array_key_exists('checkbox', $idProd)) {
        echo "$orderDetId $prodId checked! <br />";
    } else {
        echo "$orderDetId $prodId NOT checked! <br />";
    }
  }
}

Upvotes: 1

Related Questions